johnsmith153 Posted November 24, 2010 Share Posted November 24, 2010 I am using mysqli, OO, to connect to MySQL. I have only today started looking at this and am used to: <?php $con = mysql_connect();//etc mysql_close($connection); ?> Am I right that with mysqli (OO) that I don't need to set a connection variable wither when connecting or closing?? <?php mysqli::connect();//etc mysqli::close(); ?> What about with multiple databases, does mysqli keep track for me, as I am used to this: <?php $con1 = mysql_connect();//db1 $con2 = mysql_connect();//db2 ?> //etc Quote Link to comment https://forums.phpfreaks.com/topic/219717-oo-mysqli/ Share on other sites More sharing options...
Mchl Posted November 24, 2010 Share Posted November 24, 2010 When you use OO interface of mysqli you do $connection = new mysqli(..); so with more connections you do $connection1 = new mysqli(...); $connection2 = new mysqli(...); where $connection1 and $connection2 are MySQLi objects, that 'know' which connection to use. Quote Link to comment https://forums.phpfreaks.com/topic/219717-oo-mysqli/#findComment-1139055 Share on other sites More sharing options...
johnsmith153 Posted November 24, 2010 Author Share Posted November 24, 2010 Would this do the same? class myMySQLiAccess extends mysqli { //connect mysqli::connect();//etc } $con1 = new myMySQLiAccess(); $con2 = new myMySQLiAccess(); --- And would this be how I close them (based on my example)? $con1->mysqli::close(); $con2->mysqli::close(); $con3->mysqli::close(); Quote Link to comment https://forums.phpfreaks.com/topic/219717-oo-mysqli/#findComment-1139062 Share on other sites More sharing options...
Mchl Posted November 24, 2010 Share Posted November 24, 2010 Would this do the same? Not really. More like this: class myMySQLiAccess extends mysqli { public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) { parent::__construct($host, $username, $passwd, $dbname, $port, $socket); } } if you want to hardcode your access credentials (not really a good idea) it would look like this class myMySQLiAccess extends mysqli { public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) { parent::__construct('localhost', 'root', 'password', 'mydatabase'); } } $con = new myMySQLiAccess(); closing is done like this $con->close(); but in most cases you can safely let PHP close the connection for you (it does it automatically when script ends, or when the scope of $con is destroyed) Quote Link to comment https://forums.phpfreaks.com/topic/219717-oo-mysqli/#findComment-1139070 Share on other sites More sharing options...
johnsmith153 Posted November 24, 2010 Author Share Posted November 24, 2010 Thanks for all your help - I'm there I think ! Quote Link to comment https://forums.phpfreaks.com/topic/219717-oo-mysqli/#findComment-1139072 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.