我总是得到这条消息。“在一个非对象上调用成员函数errorInfo() 我用PDO连接数据库,它工作localhost服务器,但我需要连接到另一台服务器。这是我的代码:
class Database{
protected $db_name = "develop_for_me";
protected $host = "phpmyadmin.develop.forme";
protected $name = "develop_benj";
protected $password = "myname2015";
protected $conn;
function __construct()
{
try {
$this->conn = new PDO("mysql:host=" . $this->host.";dbname=".$this->db_name , $this->name, $this->password);
} catch (PDOException $e) {
$e->getMessage();
print_r($this->conn->errorInfo());
}
}
谢谢你的帮助!
答案 0 :(得分:0)
您有此消息,因为您的错误处理错误。
使构造函数正确方式
function __construct()
{
$this->conn = new PDO("mysql:host=" . $this->host.";dbname=".$this->db_name , $this->name, $this->password);
}
答案 1 :(得分:0)
<!DOCTYPE html>
<html>
<head>
<script lang="javascript">
var reusableHtml = "<div> <table border='1'> <tbody> <tr> <td>0</td> <td>${spm}</td> </tr> <tr> <td>A</td> <td>B</td> </tr> </tbody> </table></div>";
function pushReusbalecode(){
alert('1');
document.getElementById("demo1").innerHTML = reusableHtml;
document.getElementById("demo2").innerHTML = reusableHtml;
}
</script>
</head>
<body onload="pushReusbalecode()">
<div id="demo1"></div>
<br>##########################<br>
<div id="demo2"></div>
<br>##########################<br>
</body>
</html>
答案 2 :(得分:-1)
当异常发生时,它会跳到catch块并且不会发生分配。不要忘记打印/回显消息
function __construct() {
try {
$this->conn = new PDO("mysql:host=" . $this->host.";dbname=".$this->db_name , $this->name, $this->password);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
答案 3 :(得分:-2)
所以我重写了代码:
try {
$this->conn = new PDO("mysql:host=" . $this->host.";dbname=".$this->db_name , $this->name, $this->password);
} catch (PDOException $e) {
die("Could not connect to database");
}
现在工作正常。