Skip to content
PHP Logo

What steps should be taken to troubleshoot permission-related errors like "Access denied for user 'www-data'@'localhost'" in PHP scripts?

When encountering permission-related errors like "Access denied for user 'www-data'@'localhost'" in PHP scripts, the issue is typically related to incorrect database credentials or insufficient permissions for the user accessing the database. To troubleshoot this, ensure that the database credentials in your PHP script are correct and that the user has the necessary permissions to access the database.

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