When I have a situation like this:
$databaseA = new mysqli($host,$user,$pass,"databaseA");
$databaseB = new mysqli($host,$user,$pass,"databaseB");
When I define $databaseB
, does mysqli try to re-open the connection to $host
, or does it use the cached connection from $databaseA
?
Thanks
Assuming you have a good reason to use two different databases, the only way to make this work with a single connection is with a user that has privileges to access both databases. It would go something like this:
$db = new mysqli($host,$user,$pass); // connect to the MySQL server without specifying a database
mysqli_select_db('databaseA', $db); // Specify your default/most-used database
If you don't explicitly specify databaseB in your query, MySQL will use the default (databaseA). So a query that gets one column from databaseB and two from databaseA would look like this:
$query = "SELECT databaseB.table.column, table.column, table.column2";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With