PHP connect multiple mysql database sample code at the same time_PHP tutorial

WBOY
Release: 2016-07-13 10:35:41
Original
769 people have browsed it

Example:

Copy code 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. When the program is executed An error will be reported: 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 function mysql_query( ) Prototype:

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 last opened connection is used. If there is no open connection, this function will try to call the mysql_connect() function without parameters to establish a connection and use it. Query results will be cached.

In this example, since link_identifier is not specified, when executing the first sql, the previous open link, $conn2, is used by default, but in fact the first sql statement should use It is $conn1, so an error is reported, so in order to be able to link multiple mysql databases, you can use the following method:

Method 1: Specify the connection used in the mysql_query function, that is:
Copy code 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 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: Association in sql statement For the database used, the second parameter of mysql_query can be omitted at this time, that is:
Copy code The 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","db2");
mysql_select_db("db2", $conn2);

$sql = "select * from db1.ip"; //Associated 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];
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/742452.htmlTechArticleExample: Copy the code 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"...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!