What are best practices for troubleshooting PHP scripts that are not connecting to a database?

When troubleshooting PHP scripts that are not connecting to a database, first check the database credentials in the connection code to ensure they are correct. Make sure the database server is running and accessible from the PHP server. Also, check for any error messages or logs that may provide insight into the issue.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mydatabase";

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

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