Using More DBs concurrently in PHP+MySQL – a Solution

Last night Hasan provided me a new solution which seems awesome. I am describing it here

$db1 = mysql_connect("host","user","pwd")
mysql_select_db("db1", $db1);
$res1 = mysql_query("query",$db1);

$db2 = mysql_connect("host","user","pwd")
mysql_select_db("db2", $db2);
$res2 = mysql_query("query",$db2);

At this point you can only fetch records from you previous ResultSet, i.e $res1 – But you cannot execute new query in $db1, even if you supply the link as because the link was overwritten by the new db.

so at this point the following script will fail
$res3 = mysql_query("query",$db1); //this will fail

So how to solve that?

take a look below.
$db1 = mysql_connect("host","user","pwd")
mysql_select_db("db1", $db1);
$res1 = mysql_query("query",$db1);

$db2 = mysql_connect("host","user","pwd", true)
mysql_select_db("db2", $db2);
$res2 = mysql_query("query",$db2);

So mysql_connect hasa nother optional boolean parameter which indicates whether a link will be created or not. as we connect to the $db2 with this optional parameter set to ‘true’, so both link will remain live.

now the following query will execute successfully.
$res3 = mysql_query("query",$db1);

Thanks goes to Hasan for informing me abt the fourth parameter.