0

I'm finding that I can't loop through a set of mysql results more than once. Is this this something normal that I'm just not aware of?

I have 2 nested foreach loops like so:

foreach ($items as $item) {
    echo $item." ---------------<br>";
    // loop through set of results
    foreach ($mysqlresults as $result) {
        // loop through result
        echo $result." ^^^^^^^^^^<br>";
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            print_r($row);
            echo "<br>";
        }
    }
}

which outputs:

item 1 ---------------
ResourceID#1 ^^^^^^^
row 1 data
row 2 data
row 3 data
row 4 data
ResourceID#2 ^^^^^^^
row 1 data
row 2 data
row 3 data
row 4 data
ResourceID#3 ^^^^^^^
row 1 data
row 2 data
row 3 data
row 4 data
item 2 ---------------
item 3 ---------------
item 4 ---------------
item 5 ---------------

Is there a good reason why it can't reloop through the results or am I doing something wrong?

Thanks.

1 Answer 1

2

You can't because mysql_fetch_array moves a cursor through the results. You can use mysql_data_seek($result, 0) to rewind.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. Can you elaborate a bit? "Moves a cursor through the results" ... but I'm telling it to do it again...?
Also, do I have to use mysql_fetch_array for the first pass before using mysql_data_seek?
When you fetch a line, you move to the next one after, like when you are reading a text file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.