简体   繁体   English

如何连接到两个数据库

[英]How can I connect to two databases

i am trying to connect to two databases to create a search engine for a couple of my databases. 我正在尝试连接到两个数据库来为我的几个数据库创建一个搜索引擎。 Heres a test code. 这是一个测试代码。 can someone tell me what i am doing wrong or if it is possible. 有人可以告诉我我在做什么错还是有可能。 thanks. 谢谢。

mysql_connect("localhost","user","pass");
mysql_select_db("db1");
mysql_select_db("db2");

$search=mysql_query("SELECT * from db1.repairs, db2.order from db1,db2");
while($row=mysql_fetch_array($search)){
    echo $row['first_name']."&nbsp;".$row['esn']."&nbsp;".$row['order_type']."<br>";
}

You can query across databases if you specify the database name before the table name like this 如果在表名之前指定数据库名称,则可以跨数据库查询,例如:

SELECT     a.col1, b.col2 
FROM       db1.table1 AS a
INNER JOIN db2.table2 AS b ON a.someIdFromA = b.someIdFromB

As Korcholis mentions the problem is in your select. 正如Korcholis提到的那样,问题在您的选择中。 Also you do not want to use the mysql_* functions if you can avoid it. 如果可以避免的话,您也不想使用mysql_*函数。 PDO or MySqli are preferred. 首选PDO或MySqli。

Edit At least this works using MySQL. 编辑至少这在MySQL中有效。 I would bet it works for most other RDBMSes as well, but I don't have others handy to test and I can't say if this conforms to SQL standards or not. 我敢打赌它也适用于大多数其他RDBMS,但是我没有其他人可以方便地进行测试,而且我不能说这是否符合SQL标准。 Comments anyone? 有人评论吗?

You can use 您可以使用

<?php
$db1 = mysql_connect("localhost","user","pass");
$db2 = mysql_connect("remote","user","pass");

mysql_select_db("db1", $db1);
mysql_select_db("db1", $db2);

$query1 = mysql_query("USE somedatabase", $db1);
$query2 = mysql_query("USE otherdatabase", $db2);

Or try with a class that handles these connections in a different instances http://www.joni2back.com.ar/programacion/php-class-for-mysql-databases/ 或者尝试使用在不同实例中处理这些连接的类http://www.joni2back.com.ar/programacion/php-class-for-mysql-databases/

mysql_connect returns a $resource . mysql_connect返回一个$resource You can connect twice and select a database with each one (in fact, you can select a database from the connect itself), and then use each connection. 您可以连接两次并为每个数据库选择一个数据库(实际上,您可以从连接本身中选择一个数据库),然后使用每个连接。

However, your problem is that your SELECT is incorrect. 但是,您的问题是您的SELECT错误。 You are trying to select fields from tables from databases, which is not correct. 您正在尝试从数据库的表中选择字段,这是不正确的。 In fact, you cannot fetch two different databases in a so fancy way, because they are considered two sets of information independent and unrelated between them. 实际上,您不能以如此奇特的方式获取两个不同的数据库,因为它们被视为两组独立且不相关的信息。 That's why tables exist, to fit that problem. 这就是为什么存在表以解决该问题的原因。

This other answer, however, may have a solution. 但是, 其他答案可能有解决方案。

Otherwise, you could connect to each database using two mysql_connect and two resources, fetch the values, and cross them yourself. 否则,您可以使用两个mysql_connect和两个资源连接到每个数据库,获取值,然后自己交叉。 Not the best option, I know, but an answer that could fit your needs. 我知道,这不是最佳选择,但是可以满足您的需求。

PS: If you are beginning the project right now, switch to Mysqli or PDO. PS:如果您现在就开始项目,请切换到Mysqli或PDO。 Mysql is deprecated. 不建议使用Mysql。

Try to review this, and maybe you can't query a database with querying FROM : 尝试对此进行审查,也许您无法通过查询FROM查询数据库:

<?php
$con1 = mysqli_connect("$hostname", "$user1", "$password1", "$db1");
if (mysqli_connect_errno($con1)) {
    echo mysqli_connect_error();
}

$con2 = mysqli_connect("$hostname", "$user2", "$password2", "$db2");
if (mysqli_connect_errno($con2)) {
    echo mysqli_connect_error();
}

$search1 = mysqli_query($con1, "SELECT * from $db1table");

$search2 = mysqli_query($con2, "SELECT * from $db2table");

/* Other PHP codes here */

mysqli_close($con1);
mysqli_close($con2);
?>

You can even improve this code, nor minimized it! 您甚至可以改进此代码,也不能将其最小化!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM