How can one ensure that the database connection is properly established when migrating to new webspace in PHP?

When migrating to new webspace in PHP, one can ensure that the database connection is properly established by updating the database credentials in the connection file to match the new server's settings. It's important to double-check the hostname, username, password, and database name to ensure they are correct. Additionally, testing the connection after updating the credentials can help verify that the connection is working correctly.

<?php
$servername = "new_servername";
$username = "new_username";
$password = "new_password";
$dbname = "new_database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>