tags:

views:

45

answers:

2

I create a directory listing and came across this issue.

what value does mysql_query($query1) return if there is no value

My script received this message from $result, would it be alright to pass array(0)? Warning: mysql_fetch_array($result) expects parameter 1 to be resource, array given in

A: 

try:

mysql_query($query1) or die(mysql_error());
Rook
of course, that's some rough way to check the result of mysql_query. Better to show a meaningful error so that the user knows what happened. Also, the problem is not mysql_query failing (e.g. if the query is not well formed) but mysql_fetch_array failing because there are no results.In that case `mysql_num_rows` does the job, as @nc3b pointed out
nico
@nico actually displaying the results of a mysql error is a information leakage vulnerability because it allows hackers to better identify sql injection flaws in your system. However i posted it because its easy and to the point.
Rook
@The Rook: Sorry you may have misinterpreted what I was saying.I was suggesting: `if (!mysql_query($query)) echo "Doh!!! I couldn't read the DB!! Click here to go back somewhere"` :)I just don't like to put `die();` like that because the user will be stuck with a blank page not knowing what happened
nico
@nico you could log the repose in a flat file.
Rook
@The Rook: Yeah, that's also another option. Depends on the situation, really. Do the users of your website want to fiddle around with flat files when an error occurs? Probably not (probably they can't either)
nico
+1  A: 

php.net

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error

If it doesn't return FALSE, you can use mysql_num_rows to find out just how many rows were returned.

nc3b