What are common reasons for "Access denied" errors when connecting to a MySQL database in PHP?

Common reasons for "Access denied" errors when connecting to a MySQL database in PHP include incorrect username or password, insufficient privileges for the user, or incorrect hostname. To solve this issue, double-check the credentials, ensure the user has the necessary permissions, and verify the hostname and port.

<?php
$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";
?>