1
public function doesUserExist($u) {

    $this->dbConnect();

    mysql_select_db($this->database);

    $sUser = mysql_real_escape_string($u);

    $query = "SELECT username FROM $this->table WHERE username='$sUser'";
    $doesFieldExist = false;

    if (mysql_num_rows($query) > 0) {
        $doesFieldExist = true;
    }

    $this->dbDisconnect();

    return $doesFieldExist;

}

I get an error on this line (60)

if (mysql_num_rows($query) > 0) {

The error is:

Warning: mysql_num_rows() expects parameter 1 to be resource, string given in C:\Users\Tom\Dropbox\public_html\classes\database.class.php on line 60

With the query I'm using, mysql_num_rows($query) should return 1. I've googled and checked here, but can't see what I'm doing wrong.

Ayleanna
  • 183
  • 1
  • 3
  • 9
  • 2
    possible duplicate of [PHP & MySQL: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given](http://stackoverflow.com/questions/2546314/php-mysql-mysqli-num-rows-expects-parameter-1-to-be-mysqli-result-boolean) – Your Common Sense Mar 03 '12 at 18:46
  • 1
    This question is becoming a frightening trend... @tomofv when you typed in the title to your question every single 'suggested link' was a duplicate. And there are 1.6 million results on google. – Mike B Mar 03 '12 at 20:32

3 Answers3

4

You have not actually executed your query:

$query = "SELECT username FROM $this->table WHERE username='$sUser'";
$doesFieldExist = false;

// Execute the query with mysql_query()
$result = mysql_query($query);

// $result is a result resource that can be passed 
// to mysql_num_rows() unless the query failed and $result is FALSE
if ($result && mysql_num_rows($result) > 0) {
    $doesFieldExist = true;
}
Michael Berkowski
  • 260,803
  • 45
  • 432
  • 377
0

You should use the mysql_query before getting the number of rows.

Follow the Link below: http://php.net/manual/en/function.mysql-num-rows.php

For example :

$query = "SELECT username FROM $this->table WHERE username='$sUser'";
$result = mysql_query($query);  
$num_rows = mysql_num_rows($result);  
NullPoiиteя
  • 55,099
  • 22
  • 123
  • 139
insomiac
  • 5,512
  • 7
  • 43
  • 73
0

Actually you didn't executed query.

public function doesUserExist($u) {

    $this->dbConnect();

    mysql_select_db($this->database);

    $sUser = mysql_real_escape_string($u);

    $query = "SELECT username FROM $this->table WHERE username='$sUser'";
    $query = mysql_query($query);
    $doesFieldExist = false;

    if (mysql_num_rows($query) > 0) {
        $doesFieldExist = true;
    }

    $this->dbDisconnect();

    return $doesFieldExist;

}
Milap
  • 6,466
  • 8
  • 24
  • 45