2

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,

kaha
  • 1,417
  • 2
  • 17
  • 21
  • You could have a look at http://stackoverflow.com/questions/1384191/how-to-make-a-proper-mysqli-extension-class-with-prepared-statements and http://stackoverflow.com/questions/274892/how-do-you-connect-to-multiple-mysql-databases-on-a-single-webpage to get a first idea. – Quasdunk Aug 03 '11 at 17:08
  • 1
    Why 'multiple connections is bad'? What other way to connect to two ro more servers or use asynchronous queries? – Mchl Aug 03 '11 at 17:45
  • @Quasdunk thanks, I already looked over this topic and other which I found on the StackOverflow. – kaha Aug 03 '11 at 18:49
  • @Mchi, because I read it and heard couple of times it is better to have all your tables in one database, rather than having them in different databases – kaha Aug 03 '11 at 18:51
  • @user616822: This is not true. Sure, as far as a single application is concerned, usually having just one database is best. However sometimes you just need to work with several databases sitting on different servers (often running different database systems). `Multiple connections is bad` can also mean something different. In PHP application, you usually should create just one connection per database used i.e. don't create a new connection before each query - reuse existing connection) – Mchl Aug 03 '11 at 19:14

1 Answers1

2

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.

Mchl
  • 61,444
  • 9
  • 118
  • 120
  • @Mchi, Thank you very much, I looked over your code. It is pretty clear. Not sure exactly what functionality is needed. I'm currently reading about mysqli. If I will have any questions related to this I will write it here. – kaha Aug 03 '11 at 19:05
  • You should probably ask a new question with more detail. If not, remember you can edit your question. Don't add any new information in comments ;) – Mchl Aug 03 '11 at 19:15