使用pdo时非对象出错

时间:2011-10-22 23:50:26

标签: php pdo

我一直收到错误Call to a member function execute() on a non-object in /home... on line 44

第44行是最后一个$statement。注入$db,不知道它为什么不起作用。上面的查询工作正常。

public $userinfo = array();
public $time_now = 0;
public $session_time = 3600;
public $db;
public $config;

public function __construct($db,$config)
{
    $this->userinfo['ip'] = $_SERVER['REMOTE_ADDR'];
    $this->time_now = time();
    $this->db = $db;
    $this->config = $config;
    $this->userinfo['userid'] = 0;
    $this->userinfo['level'] = 0;
}
function validate_user()
{
    if ($temp_userid = $_SESSION['userid'] > 0)
    {
        $sql = "SELECT userid,level,name,email,lastactivity  
                FROM user 
                WHERE userid = ? LIMIT 1";

        $statement = $this->db->prepare($sql);
        $statement->execute(array($temp_userid));
        $sid_row = $statement->fetchObject();

        if ($sid_row)
        {
            $this->userinfo['userid'] = $sid_row->userid;
            $this->userinfo['level'] = $sid_row->level;
            $this->userinfo['name'] = $sid_row->name;
            $this->userinfo['email'] = $sid_row->email;

            if ($this->time_now-600 > $sid_row->lastactivity)
            {
                $sql = "UPDATE user SET lastactivity = ? WHERE userid = ? LIMIT 1";
                $statement = $this->db->prepare($sql);
                $statement->execute(array($this->time_now,$this->userinfo['userid']));
            }
        }
    }....................

1 个答案:

答案 0 :(得分:1)

为什么不尝试 $ statement = $ this-> db->执行