Home >Backend Development >PHP Tutorial >How to connect multiple MySQL databases in PHP_PHP tutorial

How to connect multiple MySQL databases in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:441445browse

How to connect multiple MySQL databases in PHP

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]]]]] )

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/834968.htmlTechArticleHow to connect multiple MySQL databases in PHP Sometimes, we may connect multiple MySQL in a PHP project Database requires writing multiple mysql_connect and mysql_select_db, but some children...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn