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,

5
  • You could have a look at stackoverflow.com/questions/1384191/… and stackoverflow.com/questions/274892/… to get a first idea. Commented Aug 3, 2011 at 17:08
  • 1
    Why 'multiple connections is bad'? What other way to connect to two ro more servers or use asynchronous queries? Commented Aug 3, 2011 at 17:45
  • @Quasdunk thanks, I already looked over this topic and other which I found on the StackOverflow. Commented Aug 3, 2011 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 Commented Aug 3, 2011 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) Commented Aug 3, 2011 at 19:14

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@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.
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 ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.