致命错误:无法访问空属性
我遇到了这个错误,行是这样的:
$stations=$this->$db->query('SELECT * from service_stations');
$db 变量被声明为私有,我在 __construct 函数中使用它,如下所示:
public function __construct() {
//after including the config file
$host=DB_HOST;
$dbname=DB_NAME;
$dbuser=DB_USER;
$dbpsw=DB_PASSWORD;
try{
$pdo_options[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION;
$this->db=new PDO('mysql:host='.$host.';dbname='.$dbname, $dbuser, $dbpsw, $pdo_options);
}
catch(Exception $e)
{
die('Erreur: '.$e->getMessage());
}
}
提前谢谢:)
i have got that error and the line was this :
$stations=$this->$db->query('SELECT * from service_stations');
the $db variable is declared private and i use it in the __construct function like this:
public function __construct() {
//after including the config file
$host=DB_HOST;
$dbname=DB_NAME;
$dbuser=DB_USER;
$dbpsw=DB_PASSWORD;
try{
$pdo_options[PDO::ATTR_ERRMODE]=PDO::ERRMODE_EXCEPTION;
$this->db=new PDO('mysql:host='.$host.';dbname='.$dbname, $dbuser, $dbpsw, $pdo_options);
}
catch(Exception $e)
{
die('Erreur: '.$e->getMessage());
}
}
thx in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能犯了一个错字:
You have probably made a typo:
您可能想写
$this->db
而不是$this->$db
。前者访问属性db
,后者访问属性,这些名称存储在$db
变量中。由于未定义此变量,因此您最终会访问一个空属性,如错误消息所示。You probably meant to write
$this->db
instead of$this->$db
. The former accesses the propertydb
, the latter access the property, those name is stored in the$db
variable. And as this variable is not defined, you end up accessing an empty property, as the error message indicates.