MySQL Forums
Forum List  »  PHP

Re: mysqli_real_query("CREATE USER") succeeds but does nothing
Posted by: Walter Oney
Date: December 05, 2019 12:42PM

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.

Options: ReplyQuote


Subject
Written By
Posted
Re: mysqli_real_query("CREATE USER") succeeds but does nothing
December 05, 2019 12:42PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.