1

I created 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 
2

2 Answers 2

3

try:

mysql_query($query1) or die(mysql_error());
Sign up to request clarification or add additional context in comments.

5 Comments

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 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: 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 you could log the repose in a flat file.
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)
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.