0

Possible Duplicate:
mysql_num_rows(): supplied argument is not a valid MySQL result resource

Let's say I have a function called getbyUsername inside a class file called User:

public static function getbyUsername($username) {
    $user = new User();
    $query = sprintf('SELECT CLIENT_ID, EMAIL, PASSWORD' . 'FROM %sClients WHERE USERNAME = "%s"', DB_TBL_PREFIX, mysql_real_escape_string($username, $GLOBALS['DB']));
    $result = mysql_query($query, $GLOBALS['DB']);
    if(mysql_num_rows($result)) {
        $row = mysql_fetch_assoc($result);
        $user->client_id = $row['CLIENT_ID'];
        $user->username = $username;
        $user->password = $row['PASSWORD'];
    }
    mysql_free_result($result);
    return $user;
}

and I'm going to check if a user exist by using this function

    $user = User::getbyUsername($_POST['username']);
    if($user->userid) {
       echo 'User Exist!';
    } else  {
      echo 'User does not exist';
    }

I'm getting an error from PHP and I don't know why:

mysql_num_rows(): supplied argument is not a valid MySQL result resource in...

mysql_free_result(): supplied argument is not a valid MySQL result resource in....

Can someone help me?

Community
  • 1
  • 1

3 Answers3

2

Your query seems to fail and thus mysql_query returns false that is not a valid MySQL result resource. There seems to be a space missing between the field expression PASSWORD and the FROM keyword:

'SELECT CLIENT_ID, EMAIL, PASSWORD FROM %sClients WHERE USERNAME = "%s"'
Gumbo
  • 643,351
  • 109
  • 780
  • 844
1

The query you are executing errors out and mysql_query returns false. That's why PHP complains that its return value is not a MySQL result.

One problem that I can see is that you are missing a space:

'SELECT CLIENT_ID, EMAIL, PASSWORD' . 'FROM 

                                    ^
Missing space here -----------------/
Jon
  • 428,835
  • 81
  • 738
  • 806
0

if you still get the same error then the problem is on the table prefix. if you use cpanel, table prefix is the same as username. you can fix this problem by using a empty table prefix, define('DB_TBL_PREFIX', '');