如何在一个PHP页面中连接到多个数据库? [英] How to connect to multiple databases in a single PHP page?

查看:353
本文介绍了如何在一个PHP页面中连接到多个数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何连接到多个MySQL数据库在一个网页上?

Possible Duplicate:
How do you connect to multiple MySQL databases on a single webpage?

如果我要连接到一个数据库,请执行一些查询,然后再从另一个数据库中执行另一个查询.我该怎么做?我只是

If I want to connect to one db do some query, and then later do another query from another DB. How do I do it? Do I just

 mysql_pconnect("host:3306", "user", "password") or die(mysql_error());
 mysql_select_db("Test") or die(mysql_error());

//do some query

 mysql_pconnect("host2:3306", "user", "password") or die(mysql_error());
 mysql_select_db("Test") or die(mysql_error());

//do another query

这是怎么做的?有几个问题.请注意,我使用了pconnect,这会影响在同一页面上两次调用它吗?另外,在调用第二个连接之前,我是否必须关闭第一个连接?

Is that how you do it? A couple of questions. Notice I used pconnect, does that affect calling it twice on the same page? Also, do I have to close the connection for the first one before calling the second one?

推荐答案

您需要将数据库连接链接存储在单独的变量中.例如

You need to store database connection link in separate variable. For example

 $connection_1 = mysql_connect("host:3306", "user", "password") or die(mysql_error());
 mysql_select_db("Test", $connection_1) or die(mysql_error());

 $connection_2 = mysql_pconnect("host2:3306", "user", "password") or die(mysql_error());
 mysql_select_db("Test", $connection_2) or die(mysql_error());

 mysql_query("your query", $connection_1); // run query for first connection
 mysql_query("your query", $connection_2); // run query for second connection

这篇关于如何在一个PHP页面中连接到多个数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆