I copied sql code from phpmyadmin for successfully creating a user and granting privileges and have been trying to adapt it in php. I’m automating database creation for new clients and would like to incorporate new user and privileges at the same time. I keep getting errors with the changes I’ve made so far. I’m using variables too. And I’m not quite certain what the (.) that appear near (*) mean in SQL. So it’s a bit tricky for me. Here’s what I’m working with…
// create user
$sql = "CREATE USER " . $dbName . "'_user'@'localhost' IDENTIFIED BY " . $privilege_passwd;
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error in creating new user.';
error_log($error);
exit();
}
// grant privileges
$sql = "GRANT SELECT , INSERT , UPDATE ,
DELETE ON * . * TO 'generic_user'@'localhost' IDENTIFIED BY '" . $privilege_passwd . "' WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error in granting privileges to new user.';
error_log($error);
exit();
}
// grant additional
$sql = "GRANT ALL PRIVILEGES ON '" . $dbName . "' . * TO '" . $dbName . "_user'@'localhost'";
$result = mysqli_query($link, $sql);
if (!$result)
{
$error = 'Error in granting privileges to new user.';
error_log($error);
exit();
}
Thoughts? If there’s a less verbose way of doing this as well, that would be fine too. Like I said, I just copied the successful code snippet that phpmyadmin produces after it’s done executing.