How can a PHP beginner effectively troubleshoot and resolve database connection issues in XAMPP?

Issue: A common issue for PHP beginners using XAMPP is database connection problems. To troubleshoot and resolve this, ensure that the database credentials (hostname, username, password, database name) in your PHP code match those in your XAMPP configuration file (e.g., phpMyAdmin). Also, check that the database server is running in XAMPP. PHP code snippet:

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$database = "my_database";

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

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