mysql - Can 1 PHP file connects to 2 databases? -


I am trying to connect to the database first to draw some information, then with that information in another database Update the table.

However, the second query always tries to pull from the database if I do not close the connection. If I close the connection, then it gives me a socket error.

I am using Drupal (hence my first database is Draupal Database).

Is this possible?

I want to get a price from a table and put it in my member information database

Yes, you can create multiple connections. You must specify the link within mysql_query as another argument.

Example:

  $ link1 = mysql_connect ('localhost', 'mysql_user', 'mysql_password'); If (! $ Link1) {dead ('is not connected:' Mysql_error ()); } // second fu 1 $ db_selected_1 = mysql_select_db ('foo1', $ link1); If (! $ Db_selected_1) {Dead ('can \' use foo: 'mysql_error ()); } $ Link2 = mysql_connect ('localhost', 'mysql_user', 'mysql_password'); If (! $ Link2) {Dead ('is not connected:' Mysql_error ()); } // choose foo2 $ db_selected_2 = mysql_select_db ('foo2', $ link2); If (! $ Db_selected_2) {dead ('can \' use foo: 'mysql_error ()); } // query on the first DB mysql_query ('SELECT * 1 WHERE 1', $ link1); // query on other DB mysql_query ('Choose 1 to 1 where 1', $ link2);   

If you have the same crendetials on the same server for both databases, you can do this:

  $ link = mysql_connect ('localhost ',' Mysql_user ',' mysql_password '); If (! $ Link) {Dead ('is not connected:' Mysql_error ()); } // choose foo1 $ db_selected = mysql_select_db ('foo1', $ link); // Query on first DB mysql_query ('Choose from * 1 to 1', $ link); // choose foo2 $ db_selected = mysql_select_db ('foo2', $ link); // query on other db mysql_query ('choose from' 1 to 1 ', $ link);   

But I suggest you use the mysqli _ or even PDO .

Comments