Sometimes, we may connect multiple MySQL databases in a PHP project, and we need to write multiple mysql_connect and mysql_select_db. However, some children find that after writing, they always call the last database.
Actually the correct way to write it is:
//Database 1
$conn1 = mysql_connect('localhost:3306', 'root', '111111',true);
mysql_select_db('db1', $conn1);
mysql_query ('set names UTF8');
//Database 2
$conn2 = mysql_connect('localhost:3306', 'root', '222222',true);
mysql_select_db('db2', $conn2);
mysql_query ('set names UTF8');
The principle is to set the "new_link" parameter to true in the mysql_connect function to force the use of a new connection.
mysql_connect function
Resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )