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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools