views:

47

answers:

1

I am getting this error when I pass an invalid SQL string... I spent the last hour trying to find the problem assuming - It's not my SQL it must be the db handle... ANyway, I've now figured out that it was bad SQL...

What I want to do is test the result of the mysql_query() for a valid resultset.

I am simply using empty($result)... Is this the most effective test? Is there a more widely accepted method of testing a resultset for a valid result?

+2  A: 

mysql_query will return false if there is an error

$result = mysql_query('select * from');
if ($result === false) {
    // caused by my invalid input above
} else {
    // process as usual
}

In fact, you're getting the error you describe because you're literally calling mysql_num_rows(false)

Matt
You can also test it effectively with `if(!result)`. You should consider learning the PDO or mysqli extensions however, as you are using an extension with no support for prepared statements and other goodies that should be considered baseline.
Erik
@Erik - You have valid points, however I prefer to always use strict equality (`===`). Further, I don't use the `mysql` library, OP does :)
Matt