I usually try to minimize calls to MySQL where possible, but I've finally encountered the case where I have to do multiple calls to MySQL in a single script.
It looks like you can't use mysql_fetch_assoc
twice in a single script(!).
It seems that mysql_data_seek is the solution, but I cannot seem to get it to work.
To be clear, I have two queries I need to make. The second query depends on results from the first... Here's the structure:
$result = mysql_query($query1); while($row = mysql_fetch_assoc($result)){ $pos = $row['position']; } mysql_free_result($result); // result freed per comment below. $query2 = ' '; //... dependent on $pos - in mysql shell this returns results! $result2 = mysql_query($query2) while($row = mysql_fetch_assoc($result2)){ echo $row['id']; }
What happens above is that the second while loop returns no results even though the query should have nontrivial rows.
Just to be sure: Is this how you clear the pointer from the previous result to be able to use mysql_fetch_assoc again?
mysql_data_seek($result,mysql_num_rows($result) - 1);
I'm not sure what to use as the second argument. Admittedly, I am not that clear on pointers, but it seems I should clear the pointer to 0. But I get this error:
Offset 0 is invalid for MySQL result index 8 (or the query data is unbuffered