I'm trying to GRANT ALL PRIVILEGES
with php inside a loop. Each user should have full access to their corresponding Database. But for some reason GRANT ALL PRIVILEGES
command returns false
response.
$dbArray = [new OnePDO,new TwoPDO];
$dbNames = ['ONE_PDO','TWO_PDO'];
for ($foo = 0; $foo < count($dbNames); $foo++) {
$dbName = $dbNames[$foo];
$pass = $util -> generateRandomPassword();
$userName = strtolower($dbName);
$userName = str_replace('_', '.', $userName);
$util -> execute("CREATE USER '$userName'@'localhost' IDENTIFIED BY '$pass'", $dbArray[$foo]);
$util -> execute("GRANT ALL PRIVILEGES ON $dbName.* TO '$userName'@'localhost'", $dbArray[$foo]));
$util -> execute("FLUSH PRIVILEGES", $dbArray[$foo]);
}
When trying to use the database...
Access denied for user 'one.pdo'@'localhost' to database 'ONE_PDO'
Adding this just for context...
public function execute($query, $database) {
$command = $database -> prepare($query);
try {
$command -> execute();
} catch (Exception $e) {
$command = false;
}
return $command;
}