-1

Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

Here is the nonfunctioning code:

        <?php
        mysqlLogin();
        $username = $_COOKIE['username'];
        $sql = mysql_query("SELECT * FROM `users` WHERE username!='$username'");
        if(mysql_num_rows($sql != 0)) {
            echo "<table align='center' width='15%'>";
        } else {
            echo "<center>No users found!</center>";
        }
        while($row = mysql_fetch_array($query)){

        }
    ?>

Why is it not returning a numerical value?

Community
  • 1
  • 1
Jason
  • 91
  • 1
  • 2
  • 8

3 Answers3

1

This is because the line $sql != 0 will always evaluate to true or false.

What you probably want is mysql_num_rows($sql) != 0 on line 5

adu
  • 962
  • 8
  • 15
0

If your query fails (for instance because the cookie you're reading doesn't exist and you pass a null into the query), $sql is false - so check that first befor calling mysql_num_rows.

Quasdunk
  • 14,382
  • 3
  • 35
  • 44
0

mysql_num_rows expects a resource as the first parameter. You pass it a boolean. If you trace the setting of $sql back, you find that this is because of $sql != 0, which retunrs a boolean. See also adu's answer.

Also not that you are vulnerable to SQL Injection as you pass $_COOKIE['username'] without filtering into the query. Consider using parametrized queries or mysql_escape_string'ing the value.

Bouke
  • 10,901
  • 7
  • 57
  • 95