I am using sphinx for a site search and it works great but now I am trying to connect to 2 mysql databases with the same exact structure and db2 is a continuance of db1 so all info should flow smoothly. I can easily get the results by switching the db name in the code but how can I select both at once?
here is some 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 to connect to database");
mysql_select_db($CONF['mysql_database'], $db) or die("ERROR: unable to 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 i enter a single mysql db into $CONF['mysql_database'] = "db1"; but I need to select from db1 and db2. Does anyone know how I can achieve this? Also I did not post the query because I don’t think it is very useful it is just a simple select query and I am sure you get the idea.
If the databases are hosted on the same server, you can simply pick one arbitrarily in your call to
mysql_select_db()and then qualify all table names in your SQL queries with adatabasename.prefix. You can even mix and match tables from both databases in a single query. eg.If you don’t need to write queries that involve both databases, you can simply call
mysql_select_db()again each time you need to switch databases.If the databases are hosted on different servers, you will need 2 separate calls to
mysql_connect(). In this case, make sure to pass the optional$linkparameter to allmysql_functions because the default behavior is to simply use the most recent connection, which can lead to all kinds of bugs. Since each database will have a separate connection, you will not be able to run individual queries that access both databases.