What are common reasons for the "Access denied for user" error in PHP when trying to access a database?

The "Access denied for user" error in PHP typically occurs when the username or password provided in the database connection settings is incorrect, or the user does not have the necessary permissions to access the database. To solve this issue, double-check the username, password, and database name in your connection settings, and ensure that the user has the appropriate privileges to access the database.

<?php
$servername = "localhost";
$username = "root";
$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";
?>