php-beginner Posted February 6, 2011 Share Posted February 6, 2011 Hello everyone, For two weeks now, I'm trying to get this database connection in my query. Can someone give me a solution and tell me what I've done wrong? Am I overlooking something? <?php class Mysql{ public function connect(){ $mysqli = new mysqli('localhost','root','','login'); } } class Query extends Mysql{ public function runQuery(){ $this->result = parent::connect()->query("select bla bla from bla bla"); } } $query = new Query; $query->runQuery(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/ Share on other sites More sharing options...
ignace Posted February 6, 2011 Share Posted February 6, 2011 You are not returning or saving the connection: class DbMySQLi { private $connection; public function connect() { if(is_null($this->connection)) { $this->connection = new mysqli('localhost', 'root', '', 'login'); } return $this->connection; } } class Query extends DbMySQLi { private $result; public function runQuery() { $this->result = $this->connect()->query('SELECT * FROM bla'); } } $query = new Query(); $query->runQuery(); Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170606 Share on other sites More sharing options...
php-beginner Posted February 6, 2011 Author Share Posted February 6, 2011 Thanks! So I always have to return the value that i have created for another variable? Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170613 Share on other sites More sharing options...
ignace Posted February 6, 2011 Share Posted February 6, 2011 Thanks! So I always have to return the value that i have created for another variable? No, a class is a container of variables and functions. I could establish the DB connection in the constructor and all methods would have access to the DB connection. Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170647 Share on other sites More sharing options...
php-beginner Posted February 6, 2011 Author Share Posted February 6, 2011 Thanks! So I always have to return the value that i have created for another variable? No, a class is a container of variables and functions. I could establish the DB connection in the constructor and all methods would have access to the DB connection. Then why is it necessary in this case? (sorry for the lame questions) Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170653 Share on other sites More sharing options...
ignace Posted February 6, 2011 Share Posted February 6, 2011 Then why is it necessary in this case? (sorry for the lame questions) It isn't, I just extended upon your example. Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170664 Share on other sites More sharing options...
php-beginner Posted February 6, 2011 Author Share Posted February 6, 2011 Then why is it necessary in this case? (sorry for the lame questions) It isn't, I just extended upon your example. Ah ok. But i don't want to store all methods in one class because then it is not object oriented right? So, why is this necessary in my example? Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170671 Share on other sites More sharing options...
ignace Posted February 6, 2011 Share Posted February 6, 2011 Your class can have multiple methods and class variables you just have to make sure the methods are cohesive (related) towards the class and one another. Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170726 Share on other sites More sharing options...
php-beginner Posted February 6, 2011 Author Share Posted February 6, 2011 Ok, Thankyou for your patience and solving my problem. I still have one question open . Why do i have to return the value in the example given? public function connect() { if(is_null($this->connection)) { $this->connection = new mysqli('localhost', 'root', '', 'login'); } return $this->connection; Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170763 Share on other sites More sharing options...
ignace Posted February 6, 2011 Share Posted February 6, 2011 Because in your code you call: $this->connect()->query('SELECT * FROM bla'); If you don't return the value the above wouldn't work. You could rewrite it making the connection in the constructor or something and making the connection protected and call it like: $this->connection->query('SELECT * FROM bla'); Your code would look then like: class DbMySQLi { protected $connection; public function connect() { $this->connection = new mysqli('localhost', 'root', '', 'login'); } } class Query extends DbMySQLi { private $result; public function runQuery() { $this->result = $this->connection->query('SELECT * FROM bla'); } } Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170801 Share on other sites More sharing options...
php-beginner Posted February 7, 2011 Author Share Posted February 7, 2011 By making the connection in the constructor, you mean saving it in the connection variable? This code will give me the following error: Fatal error: Call to a member function query() on a non-object What's wrong? Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170912 Share on other sites More sharing options...
ignace Posted February 7, 2011 Share Posted February 7, 2011 I forgot to add the constructor, here it is again: class DbMySQLi { protected $connection; public function __construct() { $this->connect(); } public function connect() { $this->connection = new mysqli('localhost', 'root', '', 'login'); } } class Query extends DbMySQLi { private $result; public function runQuery() { $this->result = $this->connection->query('SELECT * FROM bla'); } } Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170915 Share on other sites More sharing options...
php-beginner Posted February 7, 2011 Author Share Posted February 7, 2011 Thankyou so much! Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1170918 Share on other sites More sharing options...
php-beginner Posted February 7, 2011 Author Share Posted February 7, 2011 I have marked this problem solved. But i hope you still want to answer my last question. Why do i have to return returnQuery() ? And why don't i have to return runQuery() ? <?php class Mysql{ protected $db; public function __construct(){ $this->db = new mysqli('localhost','root','','login'); } } class Query extends Mysql{ private $result; public function runQuery($query){ $this->result = $this->db->query($query); } public function returnQuery(){ return $this->result->num_rows; } } class User{ private $query; // Instantiate Query object. public function __construct(){ $this->query = new Query; } public function login(){ $this->setQuery = "select userid from users where username = 'wouter' and password = 'test'"; $this->query->runQuery($this->setQuery); if($this->query->returnQuery() > 0){ echo "You are logged in!"; }else{ echo "Wrong username / password!"; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/226868-oo-php-plus-oo-mysqli/#findComment-1171038 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.