How to do 2 different database queries with PHP

2

I have 2 banks online, and I would like to consult the 2 banks how do I setup 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 one goes to another?

    
asked by anonymous 13.04.2017 / 15:08

2 answers

2

At the end of your query $Conn_1 ou $Conn_2 you get your connection to db, see 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 );
    
13.04.2017 / 15:17
2

The mysql_ * functions should not be used anymore if you choose MySQLi if you can not just inform the fourth argument of mysql_connect() to true, this will create a new connection. It should be passed in mysql_query() with its query.

mysql_select_db() returns a boolean saying whether or not the base could be accessed.

Do as follows:

$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 not we use functions of type mysql_ *?

    
13.04.2017 / 15:17