What are common reasons for receiving an "Access denied for user" error when trying to connect to a database in PHP?

The "Access denied for user" error in PHP typically occurs when the credentials provided in the database connection code are incorrect or do not have the necessary permissions to access the database. To solve this issue, double-check the username, password, hostname, and database name in your connection code to ensure they are correct. Additionally, make sure that the user has the appropriate privileges to access the database.

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