闲者博客 - 连接 https://bk1314.com/tag/%E8%BF%9E%E6%8E%A5/ zh-CN Thu, 24 Feb 2022 09:57:30 +0800 Thu, 24 Feb 2022 09:57:30 +0800 PHP同时连接多个mysql数据库的具体实现 https://bk1314.com/31.html https://bk1314.com/31.html Thu, 24 Feb 2022 09:57:30 +0800 飞鱼 方法一
<?php
$conn1 = mysql_connect("127.0.0.1", "root","root","db1");
mysql_select_db("Huju", $conn1);
$conn2 = mysql_connect("127.0.0.1", "root","root","db2");
mysql_select_db("yun", $conn2);

$sql = "select * from user";
$query = mysql_query($sql,$conn1); //添加连接$conn1
if($row = mysql_fetch_array($query))
echo $row[0]."\n";
 
$sql = "select * from url ";
$query = mysql_query($sql, $conn2);
if($row = mysql_fetch_array($query))
echo $row[0];
?>

方法二:在sql语句中关联所用数据库,此时可以省略mysql_query的第二个参数

<?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.user"; //关联数据库
$query = mysql_query($sql);
if($row = mysql_fetch_array($query))
echo $row[0]."\n";

$sql = "select * from db2.url ";
$query = mysql_query($sql);
if($row = mysql_fetch_array($query))
echo $row[0];
?>
]]>
0 https://bk1314.com/31.html#comments https://bk1314.com/feed/tag/%E8%BF%9E%E6%8E%A5/