What are some best practices for troubleshooting connection issues between PHP and MySQL, especially in a local development environment like XAMPP?

Issue: When encountering connection issues between PHP and MySQL in a local development environment like XAMPP, it is important to check the database credentials, ensure the MySQL service is running, and verify that the PHP extension for MySQL is enabled in the php.ini file. PHP Code Snippet:

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

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

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