php - 数据库对象不工作 (PHP) : "Call to a member function query() on a non-object"

标签 php mysql database object

我正在执行一些 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/

相关文章:

php - 是否可以在 PHP 中重载运算符?

php - 空($_POST) === FALSE 不能正常工作

mysql - 不精确搜索最近的时间值

mysql - 从当前状态查询 MySQL 获取总计数

mysql - 显示 2 个外键的值

mysql - 我如何使用 crontab 每天自动恢复 MySQL 数据库?

php - 在条件 Mysql 中使用时别名变量不起作用

php - 更新信息时 Mysql 0 被删除?

mysql - 将数据从 1 个数据库复制到另一个数据库

MySQL 评论