php - connecting to multiple databases -


i using sphinx site search , works great trying connect 2 mysql databases same exact structure , db2 continuance of db1 info should flow smoothly. can results switching db name in code how can select both @ once?

here code im using

$conf['sphinx_host'] = 'localhost'; $conf['sphinx_port'] = 9312; $conf['mysql_host'] = "localhost"; $conf['mysql_username'] = "user"; $conf['mysql_password'] = "password"; $conf['mysql_database'] = "db1"; $conf['sphinx_index'] = "index index2";   $db = mysql_connect($conf['mysql_host'],$conf['mysql_username'],$conf['mysql_password']) or die("error: unable connect database"); mysql_select_db($conf['mysql_database'], $db) or die("error: unable select database");   $sql = str_replace('$ids',implode(',',$ids),$conf['mysql_query']); $result = mysql_query($sql) or die($conf['debug']?("error: mysql query failed: ".mysql_error()):"error: please try later"); 

this code works fine if enter single mysql db $conf['mysql_database'] = "db1"; need select db1 , db2. know how can achieve this? did not post query because don't think useful simple select query , sure idea.

if databases hosted on same server, can pick 1 arbitrarily in call mysql_select_db() , qualify table names in sql queries databasename. prefix. can mix , match tables both databases in single query. eg.

select h.lastname, h.firstname, p.petname     db1.humans h     left outer join db2.pets p on p.human_id = h.id; 

if don't need write queries involve both databases, can call mysql_select_db() again each time need switch databases.

if databases hosted on different servers, need 2 separate calls mysql_connect(). in case, make sure pass optional $link parameter all mysql_ functions because default behavior use recent connection, can lead kinds of bugs. since each database have separate connection, not able run individual queries access both databases.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -