I want to connect two different databases (A,B) on localhost using php/mysql.
and after that how to query from those database (with mysql or PDO)
$link=mysql_connect("localhost", "name", "password", true);
$link1=mysql_connect("localhost", "name", "password", true);
$mysql_query($qry,$link);
$mysql_query($qry1,$link1);
Is it true?
Define your variable and call these 2 function before the query execution.
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
}
mysql is depricated use 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);
}
Yes, it is possible. $link
and $link1
will both hold a different link identifier for a database connection. Do note that the MySQL extension is deprecated . Consider using PDO or MySQLi .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.