There are two methods to connect multiple MySQL databases into a single webpage which are:
$link = mysqli_connect( “host_name”, “user_name”, “password”, “database_name” );
$link = new mysqli( “host_name”, “user_name”, “password”, “database_name” );
$pdo = new PDO( “mysql:host=host_name; dbname=database_name”, “user_name”, “password” );
<?php
// PHP program to connect multiple MySQL datbase
// into single webpage
// Connection of first database
// Database name => database1
// Default username of localhost => root
// Default password of localhost is '' (none)
$link1 = mysqli_connect("localhost", "root", "", "database1");
// Check for connection
if($link1 == true) {
echo "database1 Connected Successfully";
}
else {
die("ERROR: Could not connect " . mysqli_connect_error());
}
echo "<br>";
// Connection of first database
// Database name => database1
$link2 = mysqli_connect("localhost", "root", "", "database2");
// Check for connection
if($link2 == true) {
echo "database2 Connected Successfully";
}
else {
die("ERROR: Could not connect " . mysqli_connect_error());
}
echo "<br><br>Display the list of all Databases:<br>";
// Connection of databases
$link = mysqli_connect('localhost', 'root', '');
// Display the list of all database name
$res = mysqli_query($link, "SHOW DATABASES");
while( $row = mysqli_fetch_assoc($res) ) {
echo $row['Database'] . "<br>";
}
?>