What is the best practice for connecting to multiple MySQL databases in PHP?

When connecting to multiple MySQL databases in PHP, it is best practice to create separate connection objects for each database to avoid potential conflicts or confusion. This can be achieved by using the mysqli or PDO extension in PHP to establish connections to each database individually. By maintaining distinct connections, you can easily switch between databases and perform queries without interference.

// First database connection
$host1 = 'localhost';
$user1 = 'username1';
$password1 = 'password1';
$database1 = 'database1';

$mysqli1 = new mysqli($host1, $user1, $password1, $database1);

if ($mysqli1->connect_error) {
    die("Connection to database 1 failed: " . $mysqli1->connect_error);
}

// Second database connection
$host2 = 'localhost';
$user2 = 'username2';
$password2 = 'password2';
$database2 = 'database2';

$mysqli2 = new mysqli($host2, $user2, $password2, $database2);

if ($mysqli2->connect_error) {
    die("Connection to database 2 failed: " . $mysqli2->connect_error);
}