Jump to content

Nested while loops with MySQL Databases


khaos337

Recommended Posts

I am running through a database with the following command:while($row = mysql_fetch_array($videos))within that while loop i am running another one:while($subrow = mysql_fetch_array($passwords))the problem is, the second loop seems to only run the first time through, it won't run each time. if i simply call $subrow = mysql_fetch_array($passwords), how can I parse through it each time with a loop?

Link to comment
Share on other sites

Can I see the whole bit of code? Are you using mysqli_free_result($subrow) at the end of the nested loop?

Link to comment
Share on other sites

Can I see the whole bit of code? Are you using mysqli_free_result($subrow) at the end of the nested loop?
I am using mysql functions, not mysqli. And i do not have any kind of free result function in there.With out all the code inside, the structure looks like this:
while($row = mysql_fetch_array($videos))	{							while($subrow = mysql_fetch_array($passwords))			{											}					}					}

Link to comment
Share on other sites

I think the problem is that the inner loop works through all the values in $passwords and leaves the pointer at the last one. Are you making a second query inside the first loop based on values from the first query?

Link to comment
Share on other sites

I think the problem is that the inner loop works through all the values in $passwords and leaves the pointer at the last one. Are you making a second query inside the first loop based on values from the first query?
That is correct, and I'm fairly certain that is the problem, I'm just not sure how to get around it. I tried to stick in mysql_free_result($subrow); but that gives me the following error:Warning: mysql_free_result(): supplied argument is not a valid MySQL result resource
Link to comment
Share on other sites

Mysql_data_seek() seems to be your answer.
Link to comment
Share on other sites

Look up what the functions do in the manual. mysql_free_result frees the memory used by the result, i.e. it destroys the result. After you use mysql_free_result, then the variable is no longer a MySQL result resource. If you freed the result the first time through the loop, the second time it would give that error. mysql_data_seek does not destroy the result, it just moves the pointer. Regardless, check the manual to see how to use each function, make sure you're giving the parameters in the right order. If you think you are and it still says that, chances are the query failed. mysql_query will return false if the query failed and so you can check if the result is false, and if it is then use mysql_error to print the error message.

Link to comment
Share on other sites

Look up what the functions do in the manual. mysql_free_result frees the memory used by the result, i.e. it destroys the result. After you use mysql_free_result, then the variable is no longer a MySQL result resource. If you freed the result the first time through the loop, the second time it would give that error. mysql_data_seek does not destroy the result, it just moves the pointer. Regardless, check the manual to see how to use each function, make sure you're giving the parameters in the right order. If you think you are and it still says that, chances are the query failed. mysql_query will return false if the query failed and so you can check if the result is false, and if it is then use mysql_error to print the error message.
Ah, I was hitting the wrong variable with the functions. Got it working with the mysql_data_seek. Thanks for all the help guys!! Much appreciated
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...