我正在执行一些 PHP 并尝试查询数据库,然后对返回的行对象执行一些操作。我的页面一直在说:“调用非对象上的成员函数 query()”
这是 PHP:
public function __contruct() {
$this->db = new PDO("mysql:dbname=DBName;host=mysql.our.host", 'User_Admin_Name', 'Password');
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function getEvents() {
$sql = 'SELECT this FROM that';
$rows = $this->db->query($sql); //This is the line that the error indicates is problematic
}
有谁知道为什么它不能将 db 识别为常规 db 对象?
最佳答案
这一行:
public function __contruct() {
^
construct
这个词被拼错了 (construct),除非那是拼写错误。
立即尝试:
public function __construct() {
public function __construct() {
$this->db = new PDO("mysql:dbname=DBName;host=mysql.our.host", 'User_Admin_Name', 'Password');
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public function getEvents() {
$sql = 'SELECT this FROM that';
$rows = $this->db->query($sql); //This is the line that the error indicates is problematic
}
关于php - 数据库对象不工作 (PHP) : "Call to a member function query() on a non-object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20810362/