PHP connects to multiple mysql databases at the same time. example code _ PHP Tutorial

Source: Internet
Author: User
PHP connects to multiple mysql database sample codes at the same time. Example: Copy the code as follows :? Php $ conn1mysql_connect (127.0.0.1, root, root, db1); mysql_select_db (db1, $ conn1); $ conn2mysql_connect (127.0.0.1, root, root instance:

The code is as follows:


$ Conn1 = mysql_connect ("127.0.0.1", "root", "root", "db1 ");
Mysql_select_db ("db1", $ conn1 );
$ Conn2 = mysql_connect ("127.0.0.1", "root", "root", "db2 ");
Mysql_select_db ("db2", $ conn2 );

$ SQL = "select * from ip ";
$ Query = mysql_query ($ SQL );
If ($ row = mysql_fetch_array ($ query ))
Echo $ row [0]. "\ n ";

$ SQL = "select * from web ";
$ Query = mysql_query ($ SQL );
If ($ row = mysql_fetch_array ($ query ))
Echo $ row [0];
?>


There is a problem with this code. during program execution, the following error occurs: PHP Warning: mysql_fetch_array () expects parameter 1 to be resource, boolean given in ....

Cause analysis:

The program starts to establish two database links. the prototype of the function mysql_query () is as follows:

Resource mysql_query (string $ query [, resource $ link_identifier])

Sends a query to the active database on the server associated with the specified connection identifier. If link_identifier is not specified, the last opened connection is used. If no connection is enabled, this function will try to call the mysql_connect () function without parameters to establish a connection and use it. The query results are cached.

In this example, because link_identifier is not specified, the last opened link, namely $ conn2, is used by default when the first SQL statement is executed, in fact, the first SQL statement should use $ conn1, which leads to an error. to connect to multiple mysql databases, you can use the following method:

Method 1: specify the connection used in the mysql_query function, namely:

The code is as follows:


$ Conn1 = mysql_connect ("127.0.0.1", "root", "root", "db1 ");
Mysql_select_db ("Muma", $ conn1 );
$ Conn2 = mysql_connect ("127.0.0.1", "root", "root", "db2 ");
Mysql_select_db ("product", $ conn2 );

$ SQL = "select * from ip ";
$ Query = mysql_query ($ SQL, $ conn1); // add a connection $ conn1
If ($ row = mysql_fetch_array ($ query ))
Echo $ row [0]. "\ n ";

$ SQL = "select * from web ";
$ Query = mysql_query ($ SQL, $ conn2 );
If ($ row = mysql_fetch_array ($ query ))
Echo $ row [0];
?>


Method 2: link the database used in the SQL statement. in this case, the second parameter of mysql_query can be omitted, namely:

The code is as follows:


$ Conn1 = mysql_connect ("127.0.0.1", "root", "root", "db1 ");
Mysql_select_db ("db1", $ conn1 );
$ Conn2 = mysql_connect ("127.0.0.1", "root", "root", "db2 ");
Mysql_select_db ("db2", $ conn2 );

$ SQL = "select * from db1.ip"; // associate a database
$ Query = mysql_query ($ SQL );
If ($ row = mysql_fetch_array ($ query ))
Echo $ row [0]. "\ n ";

$ SQL = "select * from db2.web ";
$ Query = mysql_query ($ SQL );
If ($ row = mysql_fetch_array ($ query ))
Echo $ row [0];
?>

The pipeline code is as follows :? Php $ conn1 = mysql_connect ("127.0.0.1", "root", "root", "db1"); mysql_select_db ("db1", $ conn1 ); $ conn2 = mysql_connect ("127.0.0.1", "root", "root "...

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.