How can one troubleshoot PHP scripts that are unable to connect to a MySQL database?

If a PHP script is unable to connect to a MySQL database, it could be due to incorrect database credentials, server configuration issues, or network problems. To troubleshoot this, check the database connection parameters in the script, ensure the MySQL server is running and accessible, and verify that the PHP MySQL extension is enabled.

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

$conn = new mysqli($servername, $username, $password, $database);

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