How can you connect and transferring data with multiple databases using php.Only using mysql_connect to simultaneously multiple databases to create multiple databases connections.
//**************** CONNECTION -1 ************************** $connection1 = mysql_connect('server1_name', 'mysql_user', 'mysql_password') or die('Could not connect with connection1: ' . mysql_error()); // connect to the database 1 mysql_select_db('database1_name', $connection1) or die('Could not connect with database1: ' . mysql_error()); //**************** CONNECTION -2 ************************** $connection2 = mysql_connect('server2_name', 'mysql_user', 'mysql_password') or die('Could not connect with connection2: ' . mysql_error()); // connect to the database 1 mysql_select_db('database2_name', $connection2) or die('Could not connect with database2: ' . mysql_error());
You can access data with these connections to the databases as following.
// get data from database 1 $SQL1 = "SELECT * FROM users"; $result1 = mysql_query($SQL1,$connection1) or die('Could not execute query with database1: ' . mysql_error()); $row1 = mysql_fetch_array($result1); print_r($row1); echo '
'; // get data from database 2 $SQL2 = "SELECT * FROM country"; $result2 = mysql_query($SQL2,$connection2) or die('Could not execute query with database2: ' . mysql_error()); $row2 = mysql_fetch_array($result2); print_r($row2);
Note: Always is server name as IP Address.If you use localhost, then each queries would not execute.