Jump to content

OO mysqli


johnsmith153

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/219717-oo-mysqli/
Share on other sites

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.

 

Link to comment
https://forums.phpfreaks.com/topic/219717-oo-mysqli/#findComment-1139055
Share on other sites

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();

Link to comment
https://forums.phpfreaks.com/topic/219717-oo-mysqli/#findComment-1139062
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/219717-oo-mysqli/#findComment-1139070
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.