Hi,
I want to access 2 MYSQL databases from the same php script.
Without closing one and opening the other one.
I want to select queries from both databases at the same time.
Thanks
Solved: How to open 2 mysql databases in php
Solved: How to open 2 mysql databases in php
Last edited by mister_v on Wed Sep 26, 2012 1:48 pm, edited 1 time in total.
Re: How to open 2 mysql databases in php
You can add a 4th parameter in mysql_connect,
this will open a new separated connection.
Perhaps a better way is to use mysqli
(You need to compile php with it)
this will open a new separated connection.
Code: Select all
$dbconn1 = mysql_connect($hostname, $username, $password);
$dconn2 = mysql_connect($hostname, $username, $password, true);
mysql_select_db('database1', $dbconn1);
mysql_select_db('database2', $dbconn2);
$result1=mysql_query("Select * FROM table",$dbconn1);
$line1=mysql_fetch_array($result1, MYSQL_NUM);
$result2=mysql_query("Select * FROM tableOtherDB",$dbconn2);
$line2=mysql_fetch_array($result2, MYSQL_NUM);
Perhaps a better way is to use mysqli
(You need to compile php with it)
Code: Select all
$mysqli1 = new mysqli($hostname, $username, $password, 'database1');
$mysqli2 = new mysqli($hostname, $username, $password, 'database2');
$result1=mysqli_query($mysqli1,"Select * FROM table");
$result2=mysqli_query($mysqli2,"Select * FROM tableOtherDB");
$line1=mysqli_fetch_array($result1, MYSQL_NUM);
$line2=mysqli_fetch_array($result2, MYSQL_NUM);
Re: How to open 2 mysql databases in php
Thanks
Didn't know about the 4th parameter true.
Didn't know about the 4th parameter true.