The thing is that I need to write a db class with mysqli and it should support multiple connections to different databases.
I know that multiple connections is bad, but I don't have any other choice.
If there is any good example of class which supports multiple connections?
Do you know any tips that I should take into consideration when I will start writing the class? What is the best practice in my case?
Thanks in advance,
First thing that comes to mind, is a container class, that stores MySQLi object in it.
Something like this:
class MySQLiContainer extends SplObjectStorage{
public function newConnection($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) {
$mysqli = new mysqli($host, $username, $passwd, $dbname, $port, $socket);
$this->attach($mysqli);
return $mysqli;
}
}
//usage
$mysqliContainer = new MySQLiContainer();
$c1 = $mysqliContainer->newConnection('localhost','root','root','localDatabase');
$c1->query('SELECT ....');
$c2 = $mysqliContainer->newConnection('mysql.remotehost.net','hackermom','bobbytables','schoolDatabase');
$name = 'Robert\'); DROP TABLE students;--';
$c2->multi_query("SELECT * FROM students WHERE name = '$name'");
Without knowing more about functionality required, it's hard to say if this is a good idea though ;)
More info about SplObjectStorage class.