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

The "Access denied for user" error in PHP typically occurs when the user does not have the necessary permissions to create a new user in the database. This can be due to incorrect credentials, insufficient privileges, or a misconfiguration of the database server. To solve this issue, ensure that the user has the appropriate permissions granted in the database server settings.

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

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

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

// SQL query to create a new user
$sql = "CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';";

if ($conn->query($sql) === TRUE) {
    echo "User created successfully";
} else {
    echo "Error creating user: " . $conn->error;
}

$conn->close();
?>