0

I have a base model class that wraps mysqli. When I instantiate multiple models each one calls new mysqli(). If the parameters passed to mysqli() are the same will it be optimized for me and only use one connection?

I guess another way to ask this is...is this:

$mysqli1 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli2 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli3 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

more or less equivalent to this:

$mysqli1 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli2 = $mysqli1;
$mysqli3 = $mysqli1;

Edit: I remember reading somewhere that it does return a copy if you're already connected but I can't find it now.

4
  • Try it and see if the variables reference the same objects - My guess is probably not though because the docs specifically state that the constructor will "Open a new connection to the MySQL server" Commented Mar 26, 2012 at 17:19
  • If I tried it I wouldn't know how to determine if it's being optimized for me. Commented Mar 26, 2012 at 17:21
  • @Ryan The way to test is to var_dump($mysqli1); var_dump($mysqli2); ... You'll see a different object reference # for each one. (they won't be optimized down, you'll get a pile of different connection resources) Commented Mar 26, 2012 at 17:52
  • are you saying it's not negligible to connect more than once? Commented Mar 26, 2012 at 18:09

1 Answer 1

1

No, it will open a new connection to the database. Every time you create an instance of mysqli class, even if with the same parameters, it will create a new instance.

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

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.