How to connect to multiple MySQL Databases in PHP

Source: Internet
Author: User

How to connect to multiple MySQL Databases in PHP

Sometimes, we may connect to multiple MySQL databases in a PHP project. We need to write multiple mysql_connect and mysql_select_db. However, some children's shoes always call the last database.

In fact, the correct method is:

// Database 1

$ Conn1 = mysql_connect ('localhost: 100', 'root', '123', true );

Mysql_select_db ('db1', $ conn1 );

Mysql_query ('set names utf8 ');

// Database 2

$ Conn2 = mysql_connect ('localhost: 100', 'root', '123', 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 and force a new connection.

Mysql_connect Function

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.