0

So I have 2 classes db and user_access class.

db class:

class db {
public function db()
{
    $db = new mysqli(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_DATABASE);


    if ($db->connect_errno()) {

        echo "Error: Could not connect to database.";

        exit;

    }
    else { return $db; }

}

}

user_Access class:

   class user_access{

public $db; 

public function __construct($db){
    $this->db = $db;

}
public function check_login($login,$password){
    $password = sha1($password);
    $sql = "SELECT * FROM admins WHERE username = '$login' AND password = '$password'";
    $result = mysqli_query($this->db,$sql);
    if($result->num_rows > 1){
        echo 'ok';
    }else{
        echo "not";
    }
}

}

And the error that I'am getting:

Notice: Trying to get property of non-object in C:\xampp\htdocs\brothers.traning\profiadmin\models\login_Class.php on line 24

Can anybody tell my why can't I use mysqli in user_access class? I have tried:

$result = $this->db->query($sql);

as well but it doesn't help.

     $db = new db();
$userDB = new user_access($db->db());
$userDB->check_login('artur','ol');

I'm getting error at line 24

SammyMe
  • 85
  • 1
  • 1
  • 7
  • Please show us, how you instantiate your user_access class and on which line you get the error (Also don't mix OOP with procedural style ) – Rizier123 Jun 25 '15 at 22:32
  • I have edited my post, where do I mic OOP with procedural? – SammyMe Jun 25 '15 at 22:35
  • `$db = new mysqli(...` <- OOP style ; `mysqli_query(...` <- procedural ; `$result->num_rows` <- OOP – Rizier123 Jun 25 '15 at 22:36
  • You need to change your public db method name or just pass the $db cause php sees it as construct. http://stackoverflow.com/questions/6872915/whats-difference-between-construct-and-function-with-same-name-as-class-has – mim. Jun 25 '15 at 22:37
  • Ow yea, but when I use $this->db->query I get 'Query method not found in class' – SammyMe Jun 25 '15 at 22:37

0 Answers0