如何同时连接两个数据库mysql

时间:2016-03-12 07:17:02

标签: php mysql

我在名为connect.php的文件中使用它:

$username_mysql = "root";
$password_mysql = "";
$database_mysql = "bdd_imvert";
//$database_mysql = "imprimvert_renouvellement";
$db = @mysql_connect($hostname_mysql, $username_mysql, $password_mysql) or die(mysql_error());
mysql_select_db($database_mysql,$db);

$hostnameivert_mysql = "localhost";
$usernameivert_mysql = "root";
$passwordivert_mysql = "";
$databaseivert_mysql = "ivert";
//$database_mysql = "imprimvert_renouvellement";
$db1 = @mysql_connect($hostnameivert_mysql, $usernameivert_mysql, $passwordivert_mysql) or die(mysql_error());
mysql_select_db($databaseivert_mysql,$db1);`

我将其包含在包含以下内容的页面中

$sql = "select * from ivert_users where id=".GetSQLValueString($user_id,"int");
$re = @mysql_query($sql,$db)

但是当我这样做时出现错误:

  

' ivert.ivert_users'不存在

我不确定为什么会收到此错误,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的代码没有问题。我尝试过同样的事情,它对我有用。您的查询一定有问题。因为您的错误消息显示连接已发生但查询返回错误。请参阅这可能有所帮助。

或者它可能的第二个原因是你没有为第一个DB定义主机。

<?php 

$hostnameivert_mysql = "localhost";
$username_mysql      = "root";
$password_mysql      = "pma";
$database_mysql      = "olivenation";
$db                  = @mysql_connect($hostname_mysql, $username_mysql, $password_mysql) or die(mysql_error());

mysql_select_db($database_mysql,$db);

echo ("<pre>");print_r($db);echo("</pre>");

$hostnameivert_mysql = "localhost";
$usernameivert_mysql = "root";
$passwordivert_mysql = "pma";
$databaseivert_mysql = "practical";

$db1=@mysql_connect($hostnameivert_mysql, $usernameivert_mysql, $passwordivert_mysql) or die(mysql_error());

mysql_select_db($databaseivert_mysql,$db1);

echo ("<pre>");print_r($db1);echo("</pre>");

$sql = "select * from product_master where id=1";
$re  = @mysql_query($sql,$db);

echo ("<pre>");print_r(mysql_fetch_array($re));echo("</pre>");

$sql = "select * from users";
$re  = @mysql_query($sql,$db1);

echo ("<pre>");print_r(mysql_fetch_array($re));echo("</pre>");

?>