PDA

View Full Version : No error with mysql_query, but one with mysql_fetch_array?


Grant Palin
01-07-2005, 07:54 AM
I'm doing some work on an archive page for my blog. You can view the page at http://blog.grantpalin.com/Archive.php. Try the links.

I use some code to generate links for each month of each year that I have entries in my MySQL database. That's not in issue - that part is fine.

However, when I click one of the links, I go to a page listing the entries for that month and year. Fine. I use the following code (an extract, as I don't believe the rest is relevant):


$entry_result = mysql_query($entry_query) or die(mysql_error());

echo "<p>" . $entry_query . "</p>\n";

echo "<p>There are " . mysql_num_rows($entry_result) . " results.</p>\n";

while ($row = mysql_fetch_array($entry_result, MYSQL_ASSOC)) {
...
}

I get no errors when executing the query - note the "or die(mysql_error())" part.

The next line, showing the query, shows what I expect - a query on the database.

Line 3 shows the number of results for the query - this is ALWAYS what it is expected to be.

The final line is where it gets interesting: I get

"Warning: mysql_fetch_array(): 9 is not a valid MySQL result resource".

When more than one result is present, the number of results line shows that correctly, but only the first result is shown. I guess that's because of the error with the final line, so the following loop would only run once.

Never seen this before...Anyone know how to deal with it?

Grant Palin
01-07-2005, 11:20 PM
Bah, I figured it out. So simple!

Turns out I was doing mysql_free_result() on the query result at the bottom of the loop, INSIDE the loop. So with the query results destroyed, that's what was causing the error. I moved that line to the line AFTER the loop ends and have no problems now.

Case closed!