gpt4 book ai didi

php - Mysql 多数据库连接不起作用

转载 作者:行者123 更新时间:2023-11-30 21:57:04 26 4
gpt4 key购买 nike

我只是尝试像下面这个例子那样连接一个辅助数据库,但我不知道为什么拒绝工作。有什么想法吗?

我提到每个数据库连接都可以单独正常工作。

$db_HOST = "localhost";
$db_USER = "db_user";
$db_PASS = "db_pass";
$db_NAME1 = "db_test1";
$db_NAME2= "db_test2";

$db_LINK1 = mysql_connect($db_HOST, $db_USER, $db_PASS) or die("Technical revision. Please try again later!");
mysql_select_db($db_NAME1, $db_LINK1) or die("Couldn't select database");

$db_LINK2 = mysql_connect($db_HOST, $db_USER, $db_PASS, true) or die("Technical revision. Please try again later!");
mysql_select_db($db_NAME2, $db_LINK2) or die("Couldn't select database");

我在日志文件中得到的错误:

PHP Warning:  mysql_fetch_array() expects parameter 1 to be resource, boolean given in /config/global/variables.php on line 27

PHP Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in config/global/hello.php on line 3

谢谢!

最佳答案

执行以下操作(使用 PDO 而不是 mysql_connect,因为后者已被弃用):

$db_HOST = "localhost";
$db_USER = "db_user";
$db_PASS = "db_pass";
$db_NAME1 = "db_test1";
$db_NAME2= "db_test2";

try {
$db_LINK1 = new PDO('mysql:host='.$db_HOST.';dbname='.$db_NAME1, $db_USER, $db_PASS);
foreach($db_LINK1->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

try {
$db_LINK2 = new PDO('mysql:host='.$db_HOST.';dbname='.$db_NAME2, $db_USER, $db_PASS);
foreach($db_LINK2->query('SELECT * from FOO') as $row) {
print_r($row);
}
$dbh = null;
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}

更多信息在这里:php.net/manual/en/pdo.connections.php

如果第二个连接失败,请检查确切的错误消息。

关于php - Mysql 多数据库连接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44784964/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com