开发者

Fatal error: Cannot access empty property [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 9 years ago.

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() {
         开发者_如何学C           //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 :)


You have probably made a typo:

$stations=$this->db->query('SELECT * from service_stations');
//               ^
// No $ here ----/


You probably meant to write $this->db instead of $this->$db. The former accesses the property db, 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜