What steps can be taken to troubleshoot and resolve issues related to database connectivity and unknown database errors in PHP scripts?

Issue: Database connectivity issues and unknown database errors in PHP scripts can often be resolved by checking the database credentials, ensuring the database server is running, and verifying the database exists and is accessible.

// Check database credentials
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "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";