Here in this tutorial I'm going to explain how you can connect multiple mysql databasees using PHP and how can you query with multiple database connections in PHP.
Here we have single instance with multiple database connections.
If you are going to use MYSQLi with PHP you can create multiple connections like below. Here we have 2 database connections.
1
2
|
$conn1 = new mysqli($hostname, $username1, $password1,$db1);
$conn2 = new mysqli($hostname, $username2, $password2,$db2);
|
Here we are querying with same table names but with different databases.
1
2
|
mysqli_query($conn1,"SELECT * FROM table");
mysqli_query($conn2,"SELECT * FROM table");
|
If you are going to use PDO with PHP then you can create database connections like below.
1
2
|
$conn1 = new PDO("mysql:host=$hostname;dbname=db1", $username1, $password1);
$conn2 = new PDO("mysql:host=$hostname;dbname=db2", $username2, $password2);
|
Here is the way to querying with same table names but with different databases using PDO.
1
2
|
$conn1->query("SELECT * FROM table");
$conn2->query("SELECT * FROM table");
|
...