gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:53 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com