两个数据库连接:php + mysql
我在同一台服务器上有两个数据库,具有相同的用户名和密码。现在我只连接到一个数据库,但我想连接到两个数据库。
现在这是我的代码,仅连接到一个数据库:
connect1.php
<?
$servername='localhost';
$dbusername='user';
$dbpassword='pass';
$dbname1='db1';
$dbname2='db2';
$link1 = connecttodb($servername,$dbname1,$dbusername,$dbpassword);
$link2 = connecttodb($servername,$dbname2,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbusername,$dbpassword)
{
$link=mysql_connect ("$servername","$dbusername","$dbpassword",TRUE);
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
return $link;
}
?>
我使用以下代码在 result.php 中显示结果:
<?
require "connect1.php";
$q=mysql_query("select * from table1 where username='test' order by id",link1);
while($nt=mysql_fetch_array($q)){
echo "$nt[location]";
}
?>
我想在 result.php 中显示类似的数据,但连接到 db2
我该怎么做?谢谢你!
I have two databases in same server with same username and pass. Right now I am connecting to only one database, but I would like to connect to both.
For now this is my code, whick connect only to one database:
connect1.php
<?
$servername='localhost';
$dbusername='user';
$dbpassword='pass';
$dbname1='db1';
$dbname2='db2';
$link1 = connecttodb($servername,$dbname1,$dbusername,$dbpassword);
$link2 = connecttodb($servername,$dbname2,$dbusername,$dbpassword);
function connecttodb($servername,$dbname,$dbusername,$dbpassword)
{
$link=mysql_connect ("$servername","$dbusername","$dbpassword",TRUE);
if(!$link){die("Could not connect to MySQL");}
mysql_select_db("$dbname",$link) or die ("could not open db".mysql_error());
return $link;
}
?>
I display result in result.php with this code:
<?
require "connect1.php";
$q=mysql_query("select * from table1 where username='test' order by id",link1);
while($nt=mysql_fetch_array($q)){
echo "$nt[location]";
}
?>
I would like to display similar data in result.php, but with connection to db2
How can I do that? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要一个全局 $link 变量,而是需要两个:
当然,将 connectodb() 更改为:
请注意,我已向 mysql_connect 添加了第四个参数,将 new_link 参数声明为 TRUE(仅当两个数据库驻留在同一服务器上)。
然后,对于每个查询,您必须根据要查询的数据库指定相应的链接变量($link1 或 $link2)。
Instead of having a single global $link variable, you need two:
And of course change connectodb() to:
Please note that i've added a fourth parameter to mysql_connect, stating TRUE for the new_link parameter (this will only be needed if the two databases reside on the same server).
Then, for each query you will have to specify the corresponding link variable (either $link1 or $link2) according to the database you wish to query.
您需要第二次连接,这一点至关重要,将新句柄保留在
$link2
中。因此,您首先需要添加一个新的连接函数,我建议现在通过引用参数创建全局变量:然后您需要为实际查询携带 $link1 和 $link2 变量:
然后再次对 $ 执行相同的查询链接2。
此时,最好研究一些用于查询多个数据库的循环,或者将其抽象化的类(如果您通常查询两个源)。
实际上还有一个超级惰性选项,如果两个表是等效的并且在同一服务器上运行,只是数据库名称不同。然后您可以使用 UNION ALL 附加查询:
You need to connect a second time, and this is crucial, keep the new handle in
$link2
. So you first need to add a new connect function, and I would advise creating the global variables via reference parameter now:You then need to carry around your $link1 and $link2 variables for the actualy queries:
Then do the same query again for $link2.
And at this point it might be best to investigage some loops for querying multiple databases, or a class which abstracts it away (if you generally query against two sources anyway).
Actually there is also a super-lazy option, if both tables are equivalent and running on the same server, just different database names. You could then append the query using UNION ALL: