未捕获的错误:调用...中的成员函数prepare()on ...(PDO)

时间:2017-09-20 12:02:22

标签: php pdo prepared-statement

有incomes.php(require_once(user.class.php)和user.class.php 我收到了这个错误。它只是一个简单的检查器用户角色情况,但PREPARE给了我一个错误。 我已经检查了其他答案,但似乎每个问题都有不同的解决方案,这就是我来这里的原因。

 $hostname='localhost';
    $username='root';
    $password='root';
    $db = new PDO("mysql:host=$hostname;dbname=advanceerp",$username,$password);
    User::setDatabase($db);
    Role::setDatabase($db); 

user.class.php

class User 
 {
    private $userRoles = array();
    private $db;

    //Alternatively use your own way of setting your Database connection.
    public static function setDatabase($db)
    {
        self::$db = $db;
    }

    //Populate the user object when it's created
    public function __construct($user_id)
    {
        $getUser = $this->db->prepare("SELECT id, username, email, name FROM users WHERE id = :userid ");
        $getUser->execute(array(":userid" => $userid));
        if($getUser->rowCount() == 1)
        {
            $userData = $getUser->fetch(PDO::FETCH_ASSOC);
            $this->user_id = $user_id;
            $this->username = ucfirst($userData['username']);
            $this->email = $userData['email'];
            //etc.. More data if needed
            self::loadRoles();//Initiate the userroles
        }
    }

    //Fill the array with this user's roles, it's
    protected static function loadRoles()
    {
        $fetchRoles = $this->db->prepare("SELECT user_role.role_id, role.role_name FROM user_role JOIN role ON user_role.role_id = role.role_id WHERE user_role.user_id = :user_id");
        $fetchRoles->execute(array(":user_id" => $this->user_id));

        //Populate the array
        while($row = $fetchRoles->fetch(PDO::FETCH_ASSOC))
        {
            $this->userRoles[$row["role_name"]] = Role::getRolePermissions($row["role_id"]);
        }
    }

    //Check if the user has a certain permission
    public function hasPermission($permission)
    {
        //If the user has more roles, check them too
        foreach ($this->userRoles as $role)
        {
            //Do the actual checking
            if ($role->hasPermission($permission))
            {
                return true;
            }
        }
        return false;
    }}

0 个答案:

没有答案