Home  >  Article  >  php教程  >  PHP中连接多个MySQL数据库的方法

PHP中连接多个MySQL数据库的方法

WBOY
WBOYOriginal
2016-06-13 09:29:331172browse

PHP中连接多个MySQL数据库的方法

   有时候,我们可能会在一个PHP项目中连接多个MySQL数据库,就要写多个mysql_connect和mysql_select_db,但是有些童鞋发现写来写全其实总是调用最后一个数据库。

  其实正确的写法是:

  //数据库1

  $conn1 = mysql_connect('localhost:3306', 'root', '111111',true);

  mysql_select_db('db1', $conn1);

  mysql_query ('set names UTF8');

  //数据库2

  $conn2 = mysql_connect('localhost:3306', 'root', '222222',true);

  mysql_select_db('db2', $conn2);

  mysql_query ('set names UTF8');

  原理就是mysql_connect函数中设置“new_link”参数为true,强制使用新的连接。

  mysql_connect函数

  resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )

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