tags:

views:

27

answers:

2

I have a mysqli_query statement like so:

$result = mysqli_query($connection,$query) 

I am wondering: If I call mysqli_query multiple times during the execution of a script, does it use the same connection to the db? Or is a new connection established each time?

Thanks,

+2  A: 

It should use the same connection, providing you don't tell it to reconnect.

Aaron
A: 

mysql_query() (which is different from mysqli_query() but should behave the same in this regard) always uses the last opened connection if one isn't provided.

So for this:

$connection1 = mysqli_connect('host1');
$query1 = mysqli_query('SELECT column1');
$query2 = mysqli_query('SELECT column2');

$connection2 = mysqli_connect('host2');
$query3 = mysqli_query('SELECT column3');

$query and $query2 will both run on the connection to host1, and $query3 will run on the connection to host2

Pickle
The `mysqli_query` function does not default to the last opened connect, like it's predecessor `mysql_query`. It **requires** the first parameter to be a link to an open mysqli connection, or it fails with an "invalid parameter count" error. - Check the manual entry. Note that the first version listed in the manual is the OOP version, which is not a function but an object method.
Atli