Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I loop through a MySQL result set more than once using the mysql_* functions?

Tags:

php

mysql

For whatever reason, I need to go through a MySQL result set twice. Is there a way to do it?

I don't want to run the query twice and I don't want to have to rewrite the script so that it stores the rows somewhere and then reuses them later.

like image 783
A-OK Avatar asked Jun 22 '11 11:06

A-OK


People also ask

What is MySQL_ result() function?

The mysql_result() function returns the value of a field in a recordset. This function returns the field value on success, or FALSE on failure.

WHERE clause MySQL With PHP?

The WHERE Clause is used to filter only those records that are fulfilled by a specific condition given by the user. in other words, the SQL WHERE clause is used to restrict the number of rows affected by a SELECT, UPDATE or DELETE query.

What is clause in PHP?

The WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified condition. SELECT column_name(s) FROM table_name WHERE column_name operator value. To learn more about SQL, please visit our SQL tutorial.


1 Answers

This is how you can do it:

$result = mysql_query(/* Your query */); while($row = mysql_fetch_assoc($result)){  // do whatever here... }  // set the pointer back to the beginning mysql_data_seek($result, 0); while($row = mysql_fetch_assoc($result)){  // do whatever here... } 

However, I would have to say, this doesn't seem the right way to handle this. Why not do the processing within the first loop?

like image 153
Shef Avatar answered Sep 20 '22 16:09

Shef