What are common reasons for MySQL access being denied in PHP?

Common reasons for MySQL access being denied in PHP include incorrect credentials, lack of privileges for the user, or the MySQL server not allowing remote connections. To solve this issue, double-check the database credentials, ensure the user has the necessary privileges, and configure the MySQL server to allow remote connections if needed.

<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$database = "your_database";

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

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