0

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

ina
  • 19,167
  • 39
  • 122
  • 201
  • http://lt.php.net/mysql_data_seek – Aurimas Ličkus Mar 28 '12 at 18:17
  • 1
    http://stackoverflow.com/questions/5749711/why-you-should-not-use-mysql-fetch-assoc-more-than-1-time – Mark Fraser Mar 28 '12 at 18:25
  • You can call it as many times as you want, but you'll only get results if you're passing in a valid query result handle, and that result still has unfetched rows available. – Marc B Mar 28 '12 at 18:35
  • What part of the information here is unclear? http://php.net/manual/en/function.mysql-data-seek.php – Mark Fraser Mar 28 '12 at 18:59
  • i guess i am not clear what the second argument is... when i set it to 0 to clear the pointer to the first row, i get this error: `Offset 0 is invalid for MySQL result index 8 (or the query data is unbuffered` – ina Mar 28 '12 at 19:00

2 Answers2

1

Check your connection with mysql_error() and see if you're getting the "commands out of sync" error. If so, you need to call mysql_free_result() after completing the first query and before starting the second. You could also just call mysql_close() to close your database connection and then reopen a new one.

For more details, see this question.

Community
  • 1
  • 1
octern
  • 4,825
  • 21
  • 38
0

OP changed the question, so see the edit

*Deleted the posted codes here**

EDIT

After your edited your question and made clear you have actually 2 resources it looks like there is something else wrong. You don't have to worry about pointer when you use two different resources to supply mysql_fetch_assoc(). The thing with mysql_fetch_assoc() is that it takes your param ($result) by reference.

Now to answer your question:

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.

Nothing wrong with multiple SQL calls in one script. Although in general you should try to minimize the SQL calls (because they may hurt performance).

It looks like you can't use mysql_fetch_assoc twice in a single script(!).

Plain wrong. Ofc you can do it. As long as you note the above. However when you have two result sets this wouldn't be your problem.

It seems that mysql_data_seek is the solution, but I cannot seem to get it to work.

Again: this has nothing to do with it when you use two (different) result sets.

To be clear, I have two queries I need to make. The second query depends on results from the first.

This shouldn't be any problem at all. It looks like is something else wrong. Have you verified that the second query really is what you think it is? Are you sure there are records? Are you sure there aren't any (MySQL) errors. Do you have error reporting enabled? Have you tried printing out mysql_error()? To better be able to help you can you please provide your real code and not etc etc stuff? Maybe something else is going on.

Or maybe you are simply trying to run the second query inside the first loop. Which would be bad in so many ways.

Thavarith
  • 287
  • 1
  • 3
  • 13
  • what if you have a second query that you need to mysql_fetch_assoc for in the same script? – ina Mar 28 '12 at 19:09
  • Sorry to take over your answer, but @ina changed the question so it wasn't correct (anymore). Hope you don't mind. :-) – PeeHaa Mar 28 '12 at 19:30
  • the second query does indeed return records! the second query is run inside its own loop. sorry, i've been staring at this thing for hours now and it's driving me nuts! – ina Mar 28 '12 at 21:50
  • sorry - i meant, if i were to shell into mysql, the second query returns records... but somehow the php isn't pulling through... – ina Mar 28 '12 at 21:55
  • @ina I feel your pain. SO you have tried to echo the second query and copy/pasted it in shell and you got results? If yes: what does `mysql_error()` say? – PeeHaa Mar 28 '12 at 23:15
  • that's the thing, mysql_error() doesn't return anything! tried putting that in multiple locations in code... no errors show up! other than the mysql seek error: "Offset 0 is invalid for MySQL result index 6 (or the query data is unbuffered) " - but that one appears even if i don't explicitly call mysql_error(). i've also tried mysql_query or die(mysql_error()) – ina Mar 28 '12 at 23:56
  • it looks like the error has to do with the generated string not being processed. i don't understand why since when i copy and paste the same generated string, it works! http://stackoverflow.com/questions/9917773/php-mysql-generated-query-copy-pasted-to-shell-returns-results-but-no-results – ina Mar 29 '12 at 00:55
  • Yes, i agreed to what RepWhoringPeelHaa said above. Ina, if you want to know the error caused by the generated string not being processed or something, you can try to change the query2 to which can returns actual records. – Thavarith Mar 29 '12 at 03:29
  • there's no error being output via mysql_error ... it seems it has to do with a generated query. see here for details: http://stackoverflow.com/questions/9917773/php-mysql-generated-query-copy-pasted-to-shell-returns-results-but-no-results – ina Mar 29 '12 at 04:49