Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.