Mysql PHP

Best way to loop through a PHP mysql Resultset


If you want to loop throuh the mysql result set twice then use below way.

$result = mysql_query("SELECT * FROM my_table");
while($row = mysql_fetch_assoc($result)) {
// inside the loop
}

The problem is, if you want to loop through the same result set again, you will get an error because the internal pointer is currently at the end of the result. You will need to put the pointer back at the beginning of the result set so that you can loop through it again.

You can do it with a helpful php function named mysql_data_seek.

mysql_data_seek($result, 0); // set the pointer of the result set back to the beginning.
while($row2 = mysql_fetch_assoc($result)) {
// inside the loop
}

How to multiple times result set fetch