arboles
arboles

Reputation: 1331

PHP error mysql_num_rows returns expects boolean error. is my query wrong?

mysql_num_rows() expects parameter 1 to be resource, boolean given in line 33

if($password!=$password_again){
            echo'password did not match';
        }else{
            $query="SELECT username FROM users WHERE 'username'='$username'";
            $query_run=mysql_query($query);


            if(mysql_num_rows($query_run)==1){
                echo'the username'.$username.'already exists';  
            }else{
                echo'ok';
            }
        }

Upvotes: 0

Views: 179

Answers (5)

Casey Kinsey
Casey Kinsey

Reputation: 1441

mysql_num_rows expects a MySQL resource. $query_run is being set to false because your query is erroring, due to the single quotes around 'username' in your WHERE clause.

Upvotes: 0

evasilchenko
evasilchenko

Reputation: 1870

You're not doing a check to see if mysql_query is returning a resource or FALSE prior to sending it to the mysql_num_rows. Please take a look at the documentation here for proper usage:

http://php.net/manual/en/function.mysql-query.php

In your case you're passing a FALSE to the mysql_num_rows function and that is why you're getting that error.

Upvotes: 0

Ryan
Ryan

Reputation: 28187

Your SELECT query looks incorrect:

$query="SELECT username FROM users WHERE 'username'='$username'";

Should probably be something like

$query="SELECT username FROM users WHERE username='$username'";

i.e. do not put the column name in single quotes. But even that is dangerous - it opens your code to SQL injection attack.

Upvotes: 0

Marc B
Marc B

Reputation: 360672

Getting a boolean back from mysql_query means there was an error - either there's a syntax error in the query (which yours has), or something else blew up (e.g. failed to connect first).

Change the query call to

        $query_run=mysql_query($query) or die(mysql_error());

The syntax error I can see is due to bad quotes

        $query="SELECT username FROM users WHERE 'username'='$username'";
                                                 ^--------^----

when you quote a field name like that, it's no longer a field name - it's just a string. However, this isn't an SQL syntax error, it's a logic error as you're doing an invalid comparison, and not comparing the real username against the username field in the database.

Upvotes: 4

crush
crush

Reputation: 17013

'username'='$username' Change this to username='$username'

Upvotes: 4

Related Questions