Click here to Skip to main content
15,616,520 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
line 35 : if (mysqli_num_rows($usernameQuery) > 0) {
echo "Username is already used: Pick another username";

I've written a registration code but upon finishing and i was brought the following error :
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in C:\wamp64\www\loginregister\register.php on line 35

What I have tried:

require "conn.php";

$username = $_POST["$username"];
$email = $_POST["$email"];
$password = $_POST["$pass"];
$mobile = $_POST["$mobile"];
$gender = $_POST["$gender"];

$isValidEmail = filter_var($email,FILTER_VALIDATE_EMAIL);

if ($conn){
  if (strlen($password) < 6 || strlen($password) > 40) {
    // code...
    echo "password should be more than 6 characters";
  }elseif ($isValidEmail === false ) {
    // code...
    echo "Please write a valid email";
  else {
    $sqlCheckUsername = "SELECT * FROM 'users_table' WHERE 'username' LIKE '$username' ";
    $usernameQuery = mysqli_query($conn,$sqlCheckUsername);

    $sqlCheckEmail = "SELECT * FROM 'users_table' WHERE 'email' LIKE '$email' ";
    $usernameQuery = mysqli_query($conn,$sqlCheckEmail);

    if (mysqli_num_rows($usernameQuery) > 0) {
      echo "Username is already used: Pick another username";
    }elseif (mysqli_num_rows($usernameQuery) > 0) {
      echo "email is already registered";
    else {
      $sql_register = "INSERT INTO 'users_table'(`username`,`email`,`password`,`mobile`,`gender`) VALUES ('$username','$email','$password','$mobile','$gender')";
    if (mysqli_query($conn,$sql_register)) {
      echo "Registration Successful";
    else {
      echo "Failed to Register";
else {
  echo "connection error: Check Your Connection and Try again";

Updated 7-Aug-20 5:58am
Richard Deeming 7-Aug-20 11:27am    
Your code is vulnerable to SQL Injection[^]. NEVER use string concatenation / interpolation to build a SQL query. ALWAYS use a parameterized query.

PHP: SQL Injection - Manual[^]
PHP: Prepared statements and stored procedures - Manual[^]

As to your question, pasting the error into Google will give you the answer. Or just look at the "related questions" on the right-hand side of the page.
Richard Deeming 7-Aug-20 11:27am    
Also, you're storing passwords in plain text. Don't do that.

PHP: password_hash[^]
PHP: password_verify[^]
ericko_254 7-Aug-20 11:43am    
Thank you

1 solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900