Re: mysqli_real_query("CREATE USER") succeeds but does nothing
mysqli_real_query($this, "CREATE USER $username identified by $password");
or, more verbosely,
public function createNewUser($username, $password)
{
$result = false;
try
{
$stmt = mysqli_stmt_init($this);
if (mysqli_stmt_prepare($stmt, "SELECT User FROM mysql.user WHERE User=?")
&& mysqli_stmt_bind_param($stmt, "s", $username)
&& mysqli_stmt_execute($stmt)
&& mysqli_stmt_bind_result($stmt, $clean_username)
&& !mysqli_stmt_fetch($stmt))
{
mysqli_stmt_close($stmt);
$stmt = mysqli_stmt_init($this);
// Use the SET command to launder the username and password strings...
$okay = mysqli_stmt_prepare($stmt, "SET @newusername=?, @newpassword=?");
if ($okay) $okay = mysqli_stmt_bind_param($stmt, "ss", $username, $password);
if ($okay) $okay = mysqli_stmt_execute($stmt);
if ($okay) $okay = mysqli_stmt_prepare($stmt, "SELECT @newusername, @newpassword");
if ($okay) $okay = mysqli_stmt_execute($stmt);
if ($okay) $okay = mysqli_stmt_bind_result($stmt, $safe_username, $safe_password);
if ($okay) $okay = mysqli_stmt_fetch($stmt);
if ($okay) mysqli_real_query($this, "CREATE USER '$safe_username' IDENTIFIED BY '$safe_password'");
{
mysqli_real_query($this, "COMMIT");
mysqli_stmt_close($stmt);
$err = mysqli_error($this);
$result = true;
}
}
else
{
mysqli_real_query($this, "ROLLBACK");
$err = mysqli_stmt_error($stmt);
}
}
catch(Exception $e)
{
error_log($e->getMessage());
echo "createNewUser failed - " . $e->getMessage();
}
return $result;
}
In both cases, there is no error message, but the user doesn't get created.