
Please share the post on your social circle if you like it. Likewise you can connect to multiple databases in php using mysqli and pdo extension. But independent of the rdbms, you need to grand a user access to a database for it to be able to do anything (like reading data) in that database. AJAX Country, State and City Dropdown List in PHP MySQL.
#Mysql join databases different servers how to

Since we have created two pdo objects, you have to set both as null.Īlthough working with pdo seems little complex than mysqli, it is very beneficial to go with it since you can switch over to a different database system in the future with minimal changes. While closing the connection, you must close all the opened connection. $stmt = $pdo2->prepare('select id, name, designation from mytable_2') $stmt = $pdo1->prepare('select id, name, email from mytable_1') Įcho $row.

SetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION) It also supports prepared statements with named parameters, thus reducing the possibility of sql injection. While($row = mysqli_fetch_assoc($result)) block. Next, select the database 'mydatabase_1' with the help of mysqli_select_db() method and display records from it as usual. Step-2) Select and Retrieve Records from the First Database Use the mysqli_connect() method to open connection to database server without specifying the name of the database. Now we will connect and fetch data from these two databases with in the same php script. All you need to do is open a single connection to the server, and switch between different databases on go with the mysqli_select_db() method.Ĭonsider this scenario, we have two databases, one is 'mydatabase_1' with a table 'mytable_1' and another is 'mydatabase_2' with a table 'mytable_2'.

Connecting Multiple Databases with PHP MySQLi:Įstablishing connection to multiple databases using mysqli api is easier than doing it with pdo.
