How to select more than one database
I want to display data in a browser. I already retrieved data from the databases, but when I call mysql_select_db
, it accepts only one database. But my data is from a different database.
How will I select them? if i am writing mysql_select_db it is taking only one data base.for asmany datatabase if i am using mysql_select_db it is开发者_如何转开发 accepting but result not displaing in browser. it is showing Maximum execution time of 30 seconds .. if i work on 1 database the results are displaying in browser but when i try to link with more than 1 database in a same conection the problem is comming.
The same problem is also comming when i am trying to link the 2 table in a single database.but if i use single table and no linking with other table than the results are comming
you need to open two different connections
$con1 = mysql_connect($server1, $user1, $pass1);
mysql_select_db($con1);
$con2 = mysql_connect($server2, $user2, $pass2);
mysql_select_db($con2);
$query1 = mysql_query($sql1, $con1);
$query2 = mysql_query($sql2, $con2);
edit:
if both your databases are accessable with one connection:
say you have two different databases, db1 and db2
$sql1 = "SELECT * FROM db1.myTable WHERE something ";
$sql2 = "SELECT * FROM db2.myOtherTable WHERE something ";
You can either qualify your queries with the database name (SELECT * FROM db1.table; SELECT * FROM db2.table)
, or you can run your first query, store its result to a PHP array, call mysql_select_db() and run the second query.
The first approach is somewhat more flexible, in that you can do things like cross-database joins if you want (can't vouch for how well a cross-database join would perform though).
First, get data from the first database and then call mysql_select_db
for the second database, and then get the values from the second database. You can move to and forth between the databases from where you require the data, just call mysql_select_db
for that database before calling the php mysql functions.
精彩评论