php - Can't get access to database in user access class -
i'm new in oop , access class db in user_access class. can't, error , can't figure out why errors that:
warning: mysqli_query() expects parameter 1 mysqli, object given in c:\xampp\htdocs\brothers.traning\profiadmin\models\login_class.php on line 20 notice: trying property of non-object in c:\xampp\htdocs\brothers.traning\profiadmin\models\login_class.php on line 21 not
this database class:
class db { public function db() { $db = new mysqli(db_server, db_username, db_password, db_database); if (mysqli_connect_errno()) { echo "error: not connect database."; exit; } } }
and im user_access class:
class user_access{ public function __construct($db){ $this->db = $db; } public function check_login($login,$password){ $password = sha1($password); $sql = "select * admins username = '$login' , password = '$password'"; $result = mysqli_query($this->db,$sql); if($result->num_rows > 1){ echo 'ok'; }else{ echo "not"; } } }
now when use:
$db = new db(); $userdb = new user_access($db); $userdb->check_login('artur','ol');
i error... can me, , tell me wrong code..?
edit3: change
$result = mysqli_query($this->db,$sql);
to
$result = $this->db->query($sql);
look manual: http://php.net/manual/en/mysqli.query.php
you working in object-oriented style methods of making queries little bit different. in first line you've used procedural-style , wrong.
edit2: must return connection object!
class db { public function db() { $db = new mysqli(db_server, db_username, db_password, db_database); if ($db->connect_errno()) { echo "error: not connect database."; exit; } else { return $db; } } }
and change way call it:
$userdb = new user_access($db->db());
edit:
class user_access{ public $db; public function __construct($db){ $this->db = $db; } public function check_login($login,$password){ $password = sha1($password); $sql = "select * admins username = '$login' , password = '$password'"; $result = mysqli_query($this->db,$sql); if($result->num_rows > 1){ echo 'ok'; }else{ echo "not"; } } }
question: in user_access class:
public function __construct($db){ $this->db = $db; }
where $db variable want access $this->db.
Comments
Post a Comment