mysql connect multiple databases in single webpage using php
you will learn how to select the records from connect multiple databases tables using the SQL SELECT query in PHP.
In this instructional exercise, utilizing three PHP APIs (API – Application Programming Interface), we’ll associate two diverse MySQL databases on a solitary PHP page. The three APIs are-
PHP’s MySQL Extension for connect multiple databases
PHP’s mysqli Extension for connect multiple databases
PHP Data Objects(PDO) for connect multiple databases
1st database database name :: onlinecode_db_one table name :: user_table_one +----+------------+-----------+-----------------------------+ | id | first_name | last_name | user_email | // column name +-----------+------------+-----------+----------------------+ | 1 | Peter | Kong | peterKong@mail.com | | 2 | John | smith | johnsmith@mail.com | | 3 | Shuvo | Kent | Shuvokent@mail.com | | 4 | nick | Habib | nickHabib@mail.com | | 5 | Anthony | Potter | Anthonypott@mail.com | +-----------+------------+-----------+----------------------+ 2st database database name :: onlinecode_db_two table name :: user_table_two +----+------------+-----------+-----------------------------+ | id | first_name | last_name | user_email | // column name +-----------+------------+-----------+----------------------+ | 1 | Peter | Kent | peterKong@mail.com | | 2 | John | Habib | johnsmith@mail.com | | 3 | Shuvo | Kong | Shuvokent@mail.com | | 4 | max | smith | johnHabib@mail.com | | 5 | Anthony | Potter | Anthonypott@mail.com | +-----------+------------+-----------+----------------------+
$database_host = 'localhost'; // database host name // if using port then add port $database_host = 'localhost:3036'; $database_user = 'database_user'; // database user name $database_pass = 'database_password_one'; // database user password $database_name = 'onlinecode_db_one'; // database name // connect with database $database_conn_one = mysql_connect($database_host, $database_user, $database_pass); // check database connection if(! $database_conn_one ) { // error in database connection die('Could not connect to database : ' . mysql_error()); } $database_host = 'localhost'; // database host name // if using port then add port $database_host = 'localhost:3036'; $database_user = 'database_user'; // database user name $database_pass = 'database_password_two'; // database user password $database_name = 'onlinecode_db_two'; // database name // connect with database $database_conn_two = mysql_connect($database_host, $database_user, $database_pass); // check database connection if(! $database_conn_two ) { // error in database connection die('Could not connect to database : ' . mysql_error()); } // select query form 1st data base "onlinecode_db_one" $sql_select_query_one = 'SELECT id,first_name,last_name,user_email FROM user_table'; mysql_select_db($database_name_one); $sql_result_one = mysql_query( $sql_select_query_one, $database_conn_one ); if(! $sql_result_one ) { // error in sql query or Fetch data die('Could not get data: ' . mysql_error()); } while($result = mysql_fetch_array($sql_result_one, MYSQL_ASSOC)) { echo "User ID :{$result['id']}". "First Name : {$result['first_name']}". "Last Name : {$result['last_name']}". "Email Address : {$result['user_email']}"; } echo "Get data successfully"; // close connection with Mysql database mysql_close($database_conn_one); // select query form 2st data base "onlinecode_db_two" $sql_select_query_two = 'SELECT id,first_name,last_name,user_email FROM user_table'; mysql_select_db($database_name_two); $sql_result_two = mysql_query( $sql_select_query_two, $database_conn_two ); if(! $sql_result_two ) { // error in sql query or Fetch data die('Could not get data: ' . mysql_error()); } while($result = mysql_fetch_array($sql_result_two, MYSQL_ASSOC)) { echo "User ID :{$result['id']}". "First Name : {$result['first_name']}". "Last Name : {$result['last_name']}". "Email Address : {$result['user_email']}"; } echo "Get data successfully"; // close connection with Mysql database mysql_close($database_conn_two);