这篇文章主要为大家详细介绍了PHP同时连接多个MySQL数据库的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小玲来看看吧!
实例:代码如下:
<?php
/* 四海网 www.q1010.com */
$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];
?>
代码如下:
<?php
/* 四海网 www.q1010.com */
$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); //添加连接$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];
?>
代码如下:
<?php
/* 四海网 www.q1010.com */
$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"; //关联数据库
$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.q1010.com/173/16559-0.html
注:关于PHP同时连接多个MySQL数据库的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。