How to connect multiple MySQL databases in PHP

Source: Internet
Author: User

Sometimes we may be able to connect multiple MySQL databases in a PHP project, write multiple mysql_connect and mysql_select_db, but some children's shoes find that writing is always called the last database.

In fact, the correct formulation is:

Database 1

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

mysql_select_db (' db1 ', $conn 1);

mysql_query (' Set names UTF8 ');

Database 2

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

mysql_select_db (' DB2 ', $conn 2);

mysql_query (' Set names UTF8 ');

The principle is that the Mysql_connect function sets the "new_link" argument to true, forcing the new connection to be used.

mysql_connect function

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

Related Article

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.