PHP simultaneously connect multiple MySQL Database sample code _php tutorial

Source: Internet
Author: User
Instance:
Copy CodeThe code is as follows:
$conn 1 = mysql_connect ("127.0.0.1", "root", "root", "db1");
mysql_select_db ("DB1", $conn 1);
$conn 2 = mysql_connect ("127.0.0.1", "root", "root", "DB2");
mysql_select_db ("DB2", $conn 2);

$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, which will be error when the program executes: PHP Warning:mysql_fetch_array () expects parameter 1 to be resource, boolean given in ....

Cause Analysis:

The program starts building two database links, function mysql_query () Prototypes:

Resource mysql_query (String $query [, Resource $link _identifier])

Sends a query to the currently active database in the server associated with the specified connection identifier. If Link_identifier is not specified, the previous open connection is used. If there is no open connection, this function attempts to call the mysql_connect () function without arguments to establish a connection and use it. The query results are cached.

In this case, because Link_identifier is not specified, the first SQL is executed by default with the previous open link, which is $conn2, and the first SQL statement should actually use $CONN1, which results in an error, So in order to be able to link multiple MySQL databases, you can use the following methods:

Method 1: Specify the connection used in the mysql_query function, i.e.:
Copy CodeThe code is as follows:
$conn 1 = mysql_connect ("127.0.0.1", "root", "root", "db1");
mysql_select_db ("Muma", $conn 1);
$conn 2 = mysql_connect ("127.0.0.1", "root", "root", "DB2");
mysql_select_db ("Product", $conn 2);

$sql = "SELECT * from IP";
$query = mysql_query ($sql, $conn 1); Add Connection $conn1
if ($row = mysql_fetch_array ($query))
echo $row [0]. " \ n ";

$sql = "SELECT * from Web";
$query = mysql_query ($sql, $conn 2);
if ($row = mysql_fetch_array ($query))
echo $row [0];
?>

Method 2: Associate the database used in the SQL statement, at which point the second parameter of mysql_query can be omitted, namely:
Copy CodeThe code is as follows:
$conn 1 = mysql_connect ("127.0.0.1", "root", "root", "db1");
mysql_select_db ("DB1", $conn 1);
$conn 2 = mysql_connect ("127.0.0.1", "root", "root", "DB2");
mysql_select_db ("DB2", $conn 2);

$sql = "SELECT * from Db1.ip"; Association 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];
?>

http://www.bkjia.com/PHPjc/742452.html www.bkjia.com true http://www.bkjia.com/PHPjc/742452.html techarticle Example: Copy code as follows: PHP $conn 1 = mysql_connect ("127.0.0.1", "root", "root", "db1"), mysql_select_db ("DB1", $conn 1); $conn 2 = 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.