How to make 2 queries in different database with PHP

Asked

Viewed 63 times

2

I have 2 banks online, and I would like to make a consultation in the 2 banks as I mount the connection?

$Conn_1 = mysql_connect("teste","teste","teste");
$Db1 = mysql_select_db("BANCO1");

$Conn_2 = mysql_connect("teste","teste","teste");
$Db2 = mysql_select_db("BANCO2");

And then how do I say which query goes to 1 and which goes to another?

  • Banks are on the same server ?

  • One location and another on Kinghost... Different servers

2 answers

2

The mysql_* functions should no longer be used should one opt for Mysqli if it is not possible simply inform the fourth argument of mysql_connect() true, this will create a new connection. It should be passed in mysql_query() with their consultation.

mysql_select_db() returns a boolean saying whether it was possible to access the database or not.

Do it this way:

$Conn_1 = mysql_connect("teste","teste","teste", true);
mysql_select_db("BANCO1", $Conn_1);
mysql_query("select ... from ... ", $Conn_1);


$Conn_2 = mysql_connect("teste","teste","teste", true);
mysql_select_db("BANCO2", $Conn_2);
mysql_query("select ... from ... ", $Conn_2);

Recommended reading:

Why should we not use mysql type functions_*?

2


At the end of your query $Conn_1 ou $Conn_2 you arrow your connection to db, see in the example below:

$Conn_1 = mysql_connect("teste","teste","teste");
$Db1 = mysql_select_db("BANCO1");

$Conn_2 = mysql_connect("teste","teste","teste");
$Db2 = mysql_select_db("BANCO2");

$Query_Banco_2 = mysql_query('SELECT * FROM tabela ', $Conn_1 );

Query_Banco_1 = mysql_query('SELECT * FROM tabela ', $Conn_2 );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.