Cep Posted December 18, 2007 Share Posted December 18, 2007 Hello! I am using the MySQLi extension to create database connections. I have created a new connection using, $db = new mysqli('server', 'user', 'pass', 'database'); I am now creating new classes that will need to utilise the database I am working with, can I make calls to $db from within these classes for new objects? Any help would be great cheers Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2007 Share Posted December 18, 2007 you can do it two ways either in each function declare it as global (not recommeded) global $db; or make it a member of the new class $objNewclass->db = $db; and then call it in the class $this->db->dbfunction(....) hope its helpful Quote Link to comment Share on other sites More sharing options...
Cep Posted December 18, 2007 Author Share Posted December 18, 2007 I'd like to take your second approach, I want to avoid the global one. Little hazy though, so $db = new mysqli('server', 'user', 'pass', 'database'); class MyClass () { public function db() { public function selecttable() { if ($result = $this->db->query('SELECT * FROM table')) { while ($row = $result->fetch_assoc()) { echo $row['field']; } } } } $newObject = new MyClass; $newObject->selecttable; } Little confused. Do you mean set it as a propertie? I thought you wrote Method. Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 <?php class MyClass () { private $db; function __construct() { $this->db = new mysqli('server', 'user', 'pass', 'database'); } public function selecttable() { if ($result = $this->db->query('SELECT * FROM table')) { while ($row = $result->fetch_assoc()) { echo $row['field']; } } } } $newObject = new MyClass; $newObject->selecttable; ?> Quote Link to comment Share on other sites More sharing options...
Cep Posted December 18, 2007 Author Share Posted December 18, 2007 Ah I see so I could not just create an object $db outside the class and then call that object I have to set a property and then create an object inside my class. I see Quote Link to comment Share on other sites More sharing options...
Cep Posted December 19, 2007 Author Share Posted December 19, 2007 Just an update to this question, I have successfully managed to adopt rajivgonsalves idea which is what I wanted to do. <?php $db = new mysqli('server', 'user', 'pass', 'database'); class MyClass () { public $db; public $field = ""; public function selecttable($id) { if ($result = $this->db->query("SELECT * FROM table WHERE id = {$id}")) { while ($row = $result->fetch_assoc()) { $this->field = $row['field']; } } } } $newObject = new MyClass; $newObject->db = $db; $newObject->selecttable(1); $field = $newObject->field; echo $field; ?> Now my question is, which method is best practice? The above or the one Thorpe mentions whereby you create a new connection for each class? Am I right in thinking that by using the above code I am creating one connection object and passing that to any class object that requires it? Or is Thorpe's method better? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 19, 2007 Share Posted December 19, 2007 you should create only one connection, however since mysql_connect overwrites the previous connection it would not make any difference ... Quote Link to comment Share on other sites More sharing options...
Cep Posted December 19, 2007 Author Share Posted December 19, 2007 I am using MySQLi I am not sure but does that the do the same? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 19, 2007 Share Posted December 19, 2007 Don't know actually I have never used mysqli but it should be the same Quote Link to comment Share on other sites More sharing options...
trq Posted December 19, 2007 Share Posted December 19, 2007 Creating a new mysqli object will create a new connection. What you really ought to do is create a singleton, so that your only ever using the one mysqli object. <?php class myMySqli { private static $instant; private function __construct() {} public static function getInstance() { if (!isset(self::$instant)) { self::$instant = new mysqli('server', 'user', 'pass', 'database'); } return self::$instant; } } class MyClass () { public $field = ""; public function selecttable($id) { if ($result = myMySqli::getInstance()->query("SELECT * FROM table WHERE id = {$id}")) { while ($row = $result->fetch_assoc()) { $this->field = $row['field']; } } } } ?> I just did'nt really want to get into this earlier. Quote Link to comment 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.