"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Can I Iterate Through a MySQL Result Set Multiple Times Using the mysql_* Functions?

Can I Iterate Through a MySQL Result Set Multiple Times Using the mysql_* Functions?

Published on 2024-12-22
Browse:162

Can I Iterate Through a MySQL Result Set Multiple Times Using the mysql_* Functions?

Reusing MySQL Result Sets with the mysql_* Functions

Question:

Is it possible to iterate through a MySQL result set multiple times using the mysql_* functions?

Background:

Sometimes, it may be necessary to process a MySQL result set twice without rerunning the query or storing its rows.

Answer:

Yes, it is possible. Here's how:

$result = mysql_query(/* Your query */);
while ($row = mysql_fetch_assoc($result)) {
    // do whatever here...
}

// reset the result set pointer to the beginning
mysql_data_seek($result, 0);

while ($row = mysql_fetch_assoc($result)) {
    // do whatever here...
}

Note:

While this method allows you to reuse the result set, it is generally not considered best practice. It is preferable to perform all necessary processing within the initial loop.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3