What steps can be taken to troubleshoot connectivity issues between PHP and a MySQL server?

To troubleshoot connectivity issues between PHP and a MySQL server, you can check if the MySQL server is running, verify the connection settings in your PHP code, ensure the correct MySQL extension is enabled in PHP, and check for any firewall or network issues blocking the connection.

<?php
$host = 'localhost';
$user = 'username';
$password = 'password';
$database = 'dbname';

$mysqli = new mysqli($host, $user, $password, $database);

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

$mysqli->close();
?>