mysql_num_rows() warning:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/30/6567330/html/loser/template/session/usersession.phpm on line 148

I get this warning every time I want to use mysql_num_rows(). Code:

$result = mysql_query("SELECT id, logged_in, user_id FROM user_session WHERE ascii_session_id = '$id'",$this->dbhandle);
if(mysql_num_rows($result)>0) {

Numerous websites I’ve googled have similar if not exact calls. Why is this throwing the warining? If You want to see the warnings. The Fatal error’d functions are not implemented yet. Is it the query itself? If so why doesn’t it throw the error? I’ve tried both forms:


Any help is much appreciated.

That error message is telling you that $result is not a valid result set.

Possible causes include

  1. syntax or structure error in your sql query

  2. you are not connected to the database when you run the query

Rewrite the code as follows, and see what shows up:

$query = "
    , logged_in
    , user_id 
  FROM user_session 
  WHERE ascii_session_id = '$id'
$result = mysql_query($query, $this->dbhandle) or die("mysql error " . mysql_error() . " in query $query");
if(mysql_num_rows($result)>0) {

Thank you kindly. I’m not selecting the db correctly apparently.

mysql error No database selected in query SELECT id , logged_in , user_id FROM user_session WHERE ascii_session_id = ‘3gjjjfjkvuvese7ufufpt0kv62’

To handle such situations in the future and a more graceful approach to “or die” should I use try/catch?

for “select” queries mysql_query() returns a resource (result set) or FALSE if an error occurred.

Therefore, an alternative to “or die()” could be a more graceful error message for the user and a link back to the home page or whatever

$rs = mysql_query($query,$conn);
if(!rs) {
    echo '<p>** Error - Server is busy.  Please try again later</p>';
    echo '<div><a href="index.php">Go to home page</a></div>';