0

having incomes.php ( require_once(user.class.php) and user.class.php i'm getting that error. It's only a simple checker user-role situation, but that PREPARE gives me an error. i've checking other answers but it seems that for each problem there is a different solution, that's why i came here.

INCOMES

 $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;
    }}
Sergio Suarez
  • 117
  • 1
  • 1
  • 10
  • The TL;DR answer here is you're mixing a static call with an instanced one. You can't do that. What you should do is pass the PDO instance into `__construct` as an argument and then you can set `$this->db` – Machavity Sep 20 '17 at 12:07
  • @Machavity please can you explain in code how to do that? i'm new in PHP PDO. That will help me a lot, i don't know how to pass the PDO instante into __construct. – Sergio Suarez Sep 20 '17 at 12:14
  • 1
    Now is the time to learn about [dependency injection](https://stackoverflow.com/questions/2255771/how-can-i-use-dependency-injection-in-simple-php-functions-and-should-i-bothe). Comes with a handy example involving a DB connector – Machavity Sep 20 '17 at 12:17
  • already read that document of dependency, problem is i don't know how to write it, i don't understand a lot of concepts :/ i' sorry, need to practice more. Can you please try to explain it to me by code si i cant figure out how it works please? Thanks in advice – Sergio Suarez Sep 20 '17 at 12:31
  • @Machavity still bloqued with that, please could you help me? – Sergio Suarez Sep 20 '17 at 14:24

0 Answers0