I'm focusing on a task requiring me to utilize multiple database connections. From things i read, I ought to have the ability to switch between connections within the query itself, something similar to:
mysql_query("SELECT * FROM user_types", $db_core)or die(mysql_error());
However I get the error:
Table 'db_company.user_types' doesn't exist
In order to view it is searching in the incorrect db, it's getting the final
I would not wish to have to re-choose the database each time but when that's the greater approach to take I'm able to.
I've the databases selected like so:
<? $currentpage = $_SERVER["REQUEST_URI"]; //Core DB $db_core_host = "localhost"; $db_core_username = "root"; $db_core_password = ""; $db_core_name = "db_main"; // $db_core = mysql_connect($db_core_host,$db_core_username,$db_core_password); mysql_select_db($db_core_name, $db_core)or die(mysql_error()); //Company DB $db_company_host = $company['db_server']; $db_company_username = $company['db_username']; $db_company_password = $company['db_password']; $db_company_name = $company['db_name']; // $db_company = mysql_connect($db_company_host,$db_company_username,$db_company_password); mysql_select_db($db_company_name, $db_company)or die(mysql_error()); ?>
Unsure whether it helps whatsoever however when I echo either from the database connections I recieve Resource id #5
Make use of the db.table syntax within the query:
mysql_query("SELECT * FROM databas_ename.table_name", $db_core) or die(mysql_error());
The code you've inside your question should work, except when both databases are on a single server. Have a look in the
$new_link parameter of
mysql_connect (see docs here): should you refer to it as two times with similar server/user/pass, the bond is going to be re-used - making you finish track of the
mysql_select_db ask one connection influence another one.
If you have two different servers, or set
true, your code should work.