What are common reasons for the error message "Benutzer '%s'@'%s' hat keine Zugriffsberechtigung" in MySQL?

The error message "Benutzer '%s'@'%s' hat keine Zugriffsberechtigung" in MySQL typically occurs when a user does not have the necessary privileges to perform a certain action on a database. To solve this issue, you can grant the required privileges to the user using the GRANT statement in MySQL.

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

// Grant privileges to the user
$sql = "GRANT SELECT, INSERT, UPDATE, DELETE ON database.* TO 'username'@'localhost'";
if ($conn->query($sql) === TRUE) {
  echo "Privileges granted successfully";
} else {
  echo "Error granting privileges: " . $conn->error;
}

// Close connection
$conn->close();
?>