来自不同主机的PHP-Mysql表联接 [英] PHP-Mysql table join from different host

查看:92
本文介绍了来自不同主机的PHP-Mysql表联接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库abc_db中,有一个表员工位于abc @ localhost(服务器),在数据库xyz_db中有另一个表部门,位于xyz @ localhost(服务器).我如何使用php mysql连接加入表.我已经编写了以下代码,但是它不会生成任何资源ID.

There is a table employee in the database abc_db at abc@localhost(server) and there is another table department in the database xyz_db at xyz@localhost(server). How can I join the tables using php mysql connection. I have written the following code but it does not generate any resource id.

$conn = mysql_connect("localhost","abc","abcdb");
$conn1 = mysql_connect("localhost","xyz","xyzdb");

$db_select=mysql_select_db("abc_db",$conn);
$db_select1=mysql_select_db("xyz_db",$conn1);

$sql="SELECT * FROM employee e LEFT JOIN department d ON e.department_id=d.id ";
$res=mysql_query($sql);

推荐答案

您不能使用与数据库的不同连接来联接两个表,而不是通过PHP,也不能在MySQL服务器上. ( @RobertPitt 有一个好处:您呢?实际上需要两个连接吗?可以在同一主机上联接两个表,但是在一个连接中的不同数据库中-假设您的连接具有访问两个表的必要特权)

You can't join two tables using different connections to the database, not from PHP, nor on the MySQL server. (@RobertPitt has a good point: do you actually need two connections? It's possible to join two tables on the same host, but in different databases, within one connection - assuming your connection has the necessary privileges to access both)

如果您可以控制一个或其他数据库,则可以尝试设置

If you have control over one or other of the databases, you might try setting up a federated table; make sure that the performance is OK though (if the db machines don't have a fast, low-latency connection (i.e. directly joined by a cable), don't bother), and there is a long list of limitations.

可能的小祸害:

  • 将表从一台服务器复制到另一台(可设置)
  • 在PHP中手动连接"它们(粗拙,效率低下,但是如果您无法控制数据库,则几乎是您的唯一选择)

这篇关于来自不同主机的PHP-Mysql表联接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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