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

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

WBOY
WBOYOriginal
2016-06-13 09:29:331123Durchsuche

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn