如何连接多个数据库,在mysql中的服务器和从两个表中查询彼此? [英] How to connect multiple database,servers in mysql and query from both tables of each other?

查看:544
本文介绍了如何连接多个数据库,在mysql中的服务器和从两个表中查询彼此?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待从不同的服务器连接两个不同的数据库。此外,我想要运行一个查询,从数据库中获取数据到单个结果。我这样做在PHP脚本与mysql。这里是我期待着这样做[不成功:)]

I am looking forward to connect two different database from different servers. Also, i would want to run a query that fetches data from both the database into a single result. Am doing this in PHP script with mysql. here is how am looking forward to do it [ without success :) ]

$dbh1 = mysql_connect('server1', 'uname', 'pwd')or die("Unable to connect to MySQL1"); 
$dbh2 = mysql_connect('server2', 'uname', 'pwd') or die("Unable to connect to MySQL2");

mysql_select_db('db1', $dbh1);
mysql_select_db('db2', $dbh2); //both will have same table name though

$qry = mysql_query("select * from db1.table1 where db1.table1.id='100' and db1.table1.id=db2.table1.id",$dbh1) or die(mysql_error());

$row= mysql_fetch_array($qry);

echo $row[2];

没有得到任何结果或任何错误。

Am not getting any result or either error. Any help is appreciated, tnx.

推荐答案

根据PHP文档:
http://il2.php.net/manual/en/function.mysql-query.php

如果未指定链接标识符,则假定由mysql_connect()打开的最后一个链接。

"If the link identifier is not specified, the last link opened by mysql_connect() is assumed."

所以在这种情况下,你只是从$ dbh2检索数据。

So in this case you're only retrieving data from $dbh2.

我不认为你可以做一个查询。您应该在获取结果后合并结果。

I don't think it's possible to do what you are trying to do with one query. You should merge the results after you get them.

这篇关于如何连接多个数据库,在mysql中的服务器和从两个表中查询彼此?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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