我想使用php / mysql在localhost上连接两个不同的数据库(A,B)。
之后如何从这些数据库(使用mysql或PDO)查询
$link=mysql_connect("localhost", "name", "password", true);
$link1=mysql_connect("localhost", "name", "password", true);
$mysql_query($qry,$link);
$mysql_query($qry1,$link1);
是真的吗?
答案 0 :(得分:0)
定义变量并在执行查询之前调用这两个函数。
function connect_host($localpass,$localuser,$localserv,$localdb)
{
if($connection == 'host')
{
return;
}
else {
$connection = 'host';
}
$con = mysql_connect($localserv,$localuser,$localpass) or die('Could not connect.');
$connection=mysql_select_db($localdb,$con);
return $connection;
}
function connect_main($mainuser,$mainpass,$mainserv,$maindb)
{
if($connection == 'main')
{
return;
}
else {
$connection = 'main';
}
$con = mysql_connect($mainserv,$mainuser,$mainpass);
if (!$con) {
$error = mysql_error();
errorlog($error . " MAIN");
die('Could not connect: ' . $error);
}
$connection=mysql_select_db($maindb,$con);
return $connection
}
答案 1 :(得分:0)
不推荐使用mysql使用mysqli
$DBServer = 'server name or IP address'; // e.g 'localhost' or '192.168.1.100'
$DBUser = 'DB_USER';
$DBPass = 'DB_PASSWORD';
$DBName1 = 'DB1_NAME';
$DBName2 = 'DB2_NAME'; //add as many you want
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName1 ); //modal connection add as many you want
// check connection
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
答案 2 :(得分:0)
是的,有可能。 $link
和$link1
都将为数据库连接保存不同的链接标识符。请注意MySQL扩展is deprecated。请考虑使用PDO或MySQLi。