Page 1 of 1

get results more than one time

Posted: Fri Jun 22, 2007 4:52 pm
by sarris
Hi there,
i am not really experienced with php and mysql.
So far i needed to get the querry results one time and i did it using that piece of code

Code: Select all

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			echo $row["id"];
		}
but i tried to do the same for more than one time as in

Code: Select all

for($i=0;$i<3;$i++){
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			echo $row["id"];
		}	
	}
and it would work only for the first of the three loops.
Anyone knows what i need to do?
THanks

Posted: Fri Jun 22, 2007 5:07 pm
by John Cartwright
Reading the documentation you'll notice for mysql_fetch_assoc()
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
So after you've iterated the result set once, the internal pointer at the end of the dataset, therefore you'll need to use mysql_data_seek()
mysql_data_seek() moves the internal row pointer of the MySQL result associated with the specified result identifier to point to the specified row number. The next call to a MySQL fetch function, such as mysql_fetch_assoc(), would return that row.