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

Common reasons for "Access denied for user" errors in PHP when connecting to a MySQL database include incorrect username or password, insufficient privileges for the user, or the user not being granted access from the host they are connecting from. To solve this issue, double-check the username and password, ensure the user has the necessary privileges, and grant access from the correct host.

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_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";
?>