php-beginner Posted February 1, 2011 Share Posted February 1, 2011 Hello everyone, I'd like to make a small object oriented login system. The problem is that I'm not very good at oop and i have only written these scripts the procedural way. So please, correct me if I'm wrong: class database - connect() class user - login() My problem is when i make a database connection in connect(), I can't use it in login(). class Database{ public function connect(){ $mysqli = new mysqli('localhost','root','','login'); } } class User{ public function login(){ // how do i use connect() from above and make a query to log the user in? } Quote Link to comment https://forums.phpfreaks.com/topic/226372-small-login-system/ Share on other sites More sharing options...
ManiacDan Posted February 1, 2011 Share Posted February 1, 2011 You need to make the database connection a member variable of the user class: class User { public function __construct() { $this->db = new Database(); $this->db->login(); } } Also: the way I've described here is inefficient, look into the singleton pattern and instanced classes in PHP. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168420 Share on other sites More sharing options...
php-beginner Posted February 1, 2011 Author Share Posted February 1, 2011 You need to make the database connection a member variable of the user class: class User { public function __construct() { $this->db = new Database(); $this->db->login(); } } Also: the way I've described here is inefficient, look into the singleton pattern and instanced classes in PHP. -Dan But, this way I am calling login() from the Database class right? Gives me: Fatal error: Call to undefined method Database::login() Quote Link to comment https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168466 Share on other sites More sharing options...
ManiacDan Posted February 1, 2011 Share Posted February 1, 2011 Yeah...that was all kinds of wrong. class User { public function __construct() { $this->db = new Database(); } public function login() { $this->db->query( $yourLoginQuery ); } } Quote Link to comment https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168518 Share on other sites More sharing options...
php-beginner Posted February 2, 2011 Author Share Posted February 2, 2011 So far, thankyou for your reactions. Unfortionatly i can't get my script to work. I know what i am doing wrong, but i can't solve this myself: <?php class Mysql{ public function __construct(){ $mysqli = new mysqli('localhost','root','','login'); } } class Query extends Mysql{ public function loginQuery(){ $query = "SELECT gebruikerid FROM gebruikers WHERE gebruikersnaam = 'wouter' AND wachtwoord = 'test'"; } } class User{ public function __construct(){ $this->mysql = new Mysql(); } public function login(){ $this->mysql->query($query); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168832 Share on other sites More sharing options...
ManiacDan Posted February 2, 2011 Share Posted February 2, 2011 With comments: <?php class Mysql { public function __construct() { //this doesn't go anywhere. Look into PHP scope. $mysqli = new mysqli( 'localhost', 'root', '', 'login' ); } } class Query extends Mysql { public function loginQuery() { //again, this does nothing and goes nowhere. $query = "SELECT gebruikerid FROM gebruikers WHERE gebruikersnaam = 'wouter' AND wachtwoord = 'test'"; } } class User { public function __construct() { $this->mysql = new Mysql(); } public function login() { //this function doesn't exist anywhere on any class, neither does $query $this->mysql->query( $query ); } } ?> -Dan Quote Link to comment https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168885 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 I have no idea how to use mysqli as I use mysql but if they have the same principals, is this what your looking for? <?php class Mysql { public function __construct() { $mysqli = new mysqli( 'localhost', 'root', '', 'login' ); } } class query extends Mysql { private $query; private $item; public function __construct($query) { $queryRun = $this->executeQuery($query); if($queryRun) { return true; } else { return $queryRun; } } public function executeQuery($query) { $this->queryReturn = mysqli_query($query); if($this->queryReturn) { return true; } else { return mysqli_error(); } } public function returnNumRow() { return mysqli_num_rows($this->queryReturn); } public function returnWhile($item) { while($row = mysqli_fetch_assoc($this->queryReturn)) { return $row[$item]; } } } class User { public function __construct() { $this->mysql = new Mysql(); } public function login() { $query = "SELECT gebruikerid FROM gebruikers WHERE gebruikersnaam = 'wouter' AND wachtwoord = 'test'"; $this->mysql->query($query); if($this->mysql->returnNumRow() == 1) { echo $this->mysql->returnWhile('username'); } else { return 'Your account does not exist'; } } } ?> James. Quote Link to comment https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168901 Share on other sites More sharing options...
php-beginner Posted February 2, 2011 Author Share Posted February 2, 2011 I have no idea how to use mysqli as I use mysql but if they have the same principals, is this what your looking for? <?php class Mysql { public function __construct() { $mysqli = new mysqli( 'localhost', 'root', '', 'login' ); } } class query extends Mysql { private $query; private $item; public function __construct($query) { $queryRun = $this->executeQuery($query); if($queryRun) { return true; } else { return $queryRun; } } public function executeQuery($query) { $this->queryReturn = mysqli_query($query); if($this->queryReturn) { return true; } else { return mysqli_error(); } } public function returnNumRow() { return mysqli_num_rows($this->queryReturn); } public function returnWhile($item) { while($row = mysqli_fetch_assoc($this->queryReturn)) { return $row[$item]; } } } class User { public function __construct() { $this->mysql = new Mysql(); } public function login() { $query = "SELECT gebruikerid FROM gebruikers WHERE gebruikersnaam = 'wouter' AND wachtwoord = 'test'"; $this->mysql->query($query); if($this->mysql->returnNumRow() == 1) { echo $this->mysql->returnWhile('username'); } else { return 'Your account does not exist'; } } } ?> James. Yes, that is exactly what i need. But this won't work right? $this->mysql->query($query); Quote Link to comment https://forums.phpfreaks.com/topic/226372-small-login-system/#findComment-1168982 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.