How to use resultcheck with prepared statement

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';
                        }
                       

ask for errors: mysqli_error()

I got it working now… I commented out an important section I think

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 think I understand what you are trying to say and have amended it…but it is still allowing me to register with the same user…


  $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);
                        $result = mysqli_stmt_get_result($stmt);
                        $num_rows = mysqli_stmt_num_rows($result);

                        if ($num_rows > 0) {
                          header("Location: ../signup.php?signup=usernametaken");
                          exit();

                        } else {
                       

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.

Your right that the php manual is great if I can understand it! I finally got it to work but I still can’t get my memberships table to work!

Ok, for the benefit of others then how about posting your revised code, to see what is different?

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 :slight_smile:

1 Like

but i guess if everything matches with my table columns, then it should work?

I got a million bucks! There was a typo!

And where was it, again for future reference?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.