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

I have seen a lot of this around the internet lately, both in forums and on some production websites (WHOOPS!) :cheesy: I am currently developing a small site which uses MySQL as it's data repository, and I am encountering this error at random. It seems to me like it occures mostly when something was mispelled in an sql query or when a variable has a type-mismatch.

In any event, I think the reason why this is happening is because I am using a wrapper class for the my_sql resources. This just makes things easier to manage if you ask me, however, the lines of code that Zend is pointing to are:

$this->result = mysql_query($sql);
$this->numberofRows = mysql_num_rows($this->result);


I think that it may be because it doesn't recognize $this->result as a valid resource, since $result seems to work.

Does anyone know one SURE way to resolve this issue, or is it different all the time?

I doubt the errors are random. It is caused by something, usually human error, 99.9% of the time ;)

I'm not too sure with your code, but be assured that the error is trying to make as much sense as possible, in this case. That's the best I can say for now.

HAHA! OMG. I got it guys. Akh. Thanks for the tips. I found out (by poking around my code) that this error is always the result of a MySQL resource being NULL or of the wrong type. It occured to me that php uses generic variables, which means that if mysql doesn't return a My_SQL resource, it is impossible for the variable $result to be initialized and as such is considered a type mismatch by MySQL.

So, this means that any time MySQL doesn't return a value, such as a query result, you will get this error message.

There are basically three common occurences of this that I have experienced:

I am trying to catch the result of an sql query that attempts to do something other than SELECT.

When I have a query that fails due to improper syntax, or typo's in the tablename or column names.

When the database connection has not been properly initialized, i.e mysql_connect, and or my_sql_select_db have not been called for some reason...

in this particular instance, I forgot parenthesis after calling $db->SelectDB();
:o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.