I am trying to get the following code to work but usually I used mysqli_query for this to work out if the username exists or not but I am getting the following errors:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\loginsystem\includes\signup2.php on line 110
SQL error
My code is:
$sql = "SELECT * FROM users WHERE user_uid = ?;";
// Create a prepared statement
$stmt = mysqli_stmt_init($conn);
//Prepare the prepared stement
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed";
} else {
//Bind parameters to the placeholder
mysqli_stmt_bind_param($stmt, "s", $uid);
$result = mysqli_stmt_prepare($stmt, $sql);
$resultCheck = mysqli_num_rows($result);
if($resultCheck > 0) {
echo ' username taken';
}
Yes, you didn’t actually execute the query in the code above.
Still has the same problem as in your other thread, though, in that you bind the parameter, then prepare the statement again, which surely kills off the binding?
I don’t think you’re using bind_param() correctly. I think it should be more like
if (!($stmt = mysqli_stmt_prepare($conn, $sql)) {
echo "Problem";
exit();
}
else {
mysqli_stmt_bind_param($stmt, "s", $uid);
mysqli_stmt_execute($stmt);
.. and so on
But I don’t know mysqli. Your $stmt seems to refer directly to the database connection, not to the prepared statement.
I got it to work by using mysqli_store_result() function… and my code ended up as follows:
$sql = "SELECT * from users where user_uid = ?;";
// Create a prepared statement
$stmt = mysqli_stmt_init($conn);
//Prepare the prepared stement
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed";
} else {
//Bind parameters to the placeholder
mysqli_stmt_bind_param($stmt, "s", $uid);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$numrows = mysqli_stmt_num_rows($stmt);
if ($numrows > 0) {
header("Location: ../signup.php?signup=usernametaken");
exit();
} else {
But if I had a million dollars, I will give it to anyone who can work out what is wrong with my memberships table as there are no errors…One of the greatest mysteries of the world!
Thanks for the post. So the other issue was the second bind_param call, basically?
The code that stops it inserting, or the table in general? I’ve already written quite a lot about what’s wrong with the design of your memberships table