Jump to content

num_rows handing me an Error


coder007

Recommended Posts

Hey,

 

Here is the snippet of code that seems to be causing the problem:

 

		$check_messages = mysql_query("SELECT mid FROM messages WHERE tuid={$_SESSION['uid']} AND read=0");
        $check_messages1 = mysql_num_rows($check_messages);

 

And here is the error:

 

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /login.php on line 98

 

 

The error appears to be in the "read=0" at the end of the mysql_query. As soon as I remove this, the query works without error. The "read" and "tuid" values are intergers, not varchar.

 

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/160072-num_rows-handing-me-an-error/
Share on other sites

Have you tried echoing out the query to see what it looks like?  (To make sure it looks as expected.)

 

 

 

Worst case you could just add "or die(mysql_error());" after the mysql_query call.  Then, if the query fails, it would tell you the error.

 

 

(You would want to take the or die() out once you've fixed the problem.)

It's possible that read is a reserved word in MySQL, so escape it in backticks.

 

<?php

$check_messages = mysql_query("SELECT mid FROM messages WHERE tuid={$_SESSION['uid']} AND `read`=0");

$check_messages1 = mysql_num_rows($check_messages);

?>

 

If it's an int field then the single quotes around the zero should have no effect.  It could also be that your $_SESSION['uid'] contains funky characters (like a single quote or semi-colon) that are terminating the statement early.

http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

 

 

 

Whoa....  It is....

 

 

In which case I would personally rename the column.  But, if you've already designed some of your program and it depends on the column name being read, that's probably not worth the trouble.

I've been bit in the butt so many times by MySQL and reserved words that all of my MySQL statements have backticks around all of my identifiers.  You especially have to worry about upgrading an existing MySQL installation to a new version where new reserved words have been introduced; in that case all sorts of queries that worked forever may break.

 

I haven't used MySQL in about a year and a half now though; we use PostgreSQL at my current company and it doesn't seem to have this problem.

I have a kind of strange habit of prefixing all of my column names.  (For example, if read where in a table named posts I probably would've named it post_read.)

 

So since I do that I hardly ever run into problems.

 

 

Looking through the reserved word list though, some of them probably come up a lot.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.