I'm getting this PHP Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource ... seen in my error_log file. And this is my code, which is the same thing I've used for a long time, and I don't believe has caused errors before. So, I wonder if the newer php doesn't allow mysql_num_rows? (My PHP Version 5.2.17) $query = "[I have a valid query here]" $query_results=mysql_query($query); $match_results=mysql_num_rows($query_results); The query works fine for displaying info on the page, and if I put 'or die' clause, it doesn't die or give any error, but just continues to add lines to the log with that PHP mysql_num_rows() warning as stated above. On official php.net it says "Use of this extension is discouraged. Instead, the MySQLi or PDO_MySQL extension should be used. " But it fails to give any examples of what I should use instead. If you click on MySQLi, it just gives a list of a bunch of functions with no syntax to replace mysql_num_rows(). Does anyone know why I might be getting this error? And what syntax someone is supposed to have besides: $match_results=mysql_num_rows($query_results) to get the rows?
The warning means that what you're passing to mysql_num_rows() isn't a valid result. So basically, mysql_query() has returned nothing so you're giving mysql_num_rows() either an error message or an empty string. The most common cause (for me) is a syntax error somewhere in the SQL (which can be difficult to find) so double check that. Otherwise, do something like: if(!$query_result = @mysql_query($query)) { echo mysql_error(); exit; } else { $rows = mysql_num_rows($query_result); } Code (markup): At least that way if there is an error, you'll see exactly what it is.
Thank you. Just curious, doesn't using @ actually suppress errors? And don't you have to use 2 equals instead of 1 in this context? And is the code there the same as:
Yeah it does but that's why you're calling mysql_error(). I prefer to do it that way so I can control when the errors are being displayed rather than have them spat out when they occur but you can do it either way. Usually you would suppress the errors and then put them in a log if they occur so that your customers/visitors aren't seeing ugly SQL errors and you can exit the script with a nice "sorry for the inconvenience" message. No because you're assigning the value to $query_result (rather than checking if $query_result is equal to a certain value). It's the same as the code you just posted but it gets it done in one step rather than assigning it ($query_result = mysql_query()) and then running it through an if statement. It's a personal preference so you can do it either way.