user3537990
user3537990

Reputation:

Check if username exists not working

So I'm using this code to check if a username exists in a database.

  $sql = $db->query("SELECT FROM people (username, password, email) WHERE username=$username");


 if(mysql_num_rows($sql)>=1)
   {
    echo"name already exists";
   }
 else
    {
    $sqll = $db->query("INSERT INTO people (username, password, created, ip) VALUES ('{$username}','{$ph}',NOW(),'{$ip}' )") or die("Error creating user");

    }

But when I run it I get

( ! ) Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in /Users/idrisk/Desktop/localhost/r.php on line 17

I'm not really sure what I should do. Is there a better way to check if the username exists?

Upvotes: 0

Views: 124

Answers (2)

Karl Viiburg
Karl Viiburg

Reputation: 832

You are getting that warning because your mysql query syntax is wrong (it's expecting a mysqli_result object as response but recieves a boolean of false).

Change:

"SELECT FROM people (username, password, email) WHERE username=$username"

To:

"SELECT username FROM people WHERE username='$username'"

You don't need the select the password and email because you are just comparing the username. Selecting the password and email for no reason is just a waste on memory.

Upvotes: 1

echo_Me
echo_Me

Reputation: 37233

change this

  $sql = $db->query("SELECT FROM people (username, password, email) WHERE username=$username");

to

  $sql = $db->query("SELECT username, password, email FROM people  WHERE username= '$username' ");

you have to select columns from table.

Upvotes: 0

Related Questions