N-Bomb(Nerd) Posted January 14, 2010 Share Posted January 14, 2010 Hi, I want to connect to my database with mysqli at the start of my class and be able to use that connection throughout my class without reconnecting again. I'm still pretty new to classes, but I can't figure this out.. I tried doing this in the in the construct, but it didn't work: $this->connection = new mysqli("localhost", "username", "password", "database"); How can I access my connection to the database anywhere in my class without reconnect? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/188461-class-question/ Share on other sites More sharing options...
Buddski Posted January 14, 2010 Share Posted January 14, 2010 What do you mean by "it didnt work"? did you get any errors at all? Quote Link to comment https://forums.phpfreaks.com/topic/188461-class-question/#findComment-994955 Share on other sites More sharing options...
JAY6390 Posted January 14, 2010 Share Posted January 14, 2010 create a var called $db in your class (like you did with the connection) Then in your __construct() method put $this->db = new mysqli(........) as you did and then to query use $this->db->query('SELECT * FROM blablah'); etc etc Quote Link to comment https://forums.phpfreaks.com/topic/188461-class-question/#findComment-994963 Share on other sites More sharing options...
ignace Posted January 14, 2010 Share Posted January 14, 2010 Your constructor should never do real work only initialization logic: class SomeClass { private $_db; public function __construct($value) { $this->_value = $value;/* <-- no work */ } public function getDb() { if (null === $this->_db) { $this->_db = new MySQLi(...); } return $this->_db; } } When you are programming remember to always lazy-load everything hold the creation of an object off until you absolutly need it. Quote Link to comment https://forums.phpfreaks.com/topic/188461-class-question/#findComment-994970 Share on other sites More sharing options...
ToonMariner Posted January 14, 2010 Share Posted January 14, 2010 you need a singleton or helper function... helper function getDB () { if(!isset($conn)) { $conn = new mysqli("localhost", "username", "password", "database"); } return $conn; } //// then anywhre in your code where you need database just: $db = getDB(); $db->query("SELECT * FROM `foo`"); ?> singleton class... <?php class DBConn { static $instance; private $_conn; private function __construct() { $this->conn = new mysqli("localhost", "username", "password", "database"); } public static function getInstance() { if ( !isset (self::$instance) } { self::$instance = new DBConn ( ); } return self::$_instance } } ///// then anywhere in your code: $db = DBConn::getInstance; $db->query("SELECT * FROM `bar`); ?> there is an over head so in most cases you would probably lean toward the helper function method... Quote Link to comment https://forums.phpfreaks.com/topic/188461-class-question/#findComment-994995 Share on other sites More sharing options...
ignace Posted January 14, 2010 Share Posted January 14, 2010 You should avoid using Singleton's as discussed here: http://www.phpfreaks.com/forums/index.php/topic,283313.msg1343157.html Quote Link to comment https://forums.phpfreaks.com/topic/188461-class-question/#findComment-995002 Share on other sites More sharing options...
ToonMariner Posted January 14, 2010 Share Posted January 14, 2010 The singleton can be a very useful design pattern... In terms of databases and where you may need a second database connection yes it will not be what you want... To get round that you can use an array in the class that stores the connection string (host,username,passord,database) and when called again see if the current connection string already exists - if so return that connection if not open a new one and return that. Quote Link to comment https://forums.phpfreaks.com/topic/188461-class-question/#findComment-995247 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.