How to loop through same recordset twice?

Is it possible to loop through the same sc_select recordset twice?

I have recordset created by: sc_select(rs, $check_sql);

Then I want to do something like this but twice:

	while(!$rs->EOF)
	{
		$datetime = $rs->fields[1];
		echo $datetime . "<br>";
		$rs->MoveNext();
	}

but it seem like once $rs reaches EOF it cannot be looped again.

Thanks

Have you tried $rs->MoveFirst(); and loop again?

yes, I’ve tried that but it doesn’t work

An sql result set is unidirectional by design. So you cannot ‘reset’ a cursor. If the result is not too big, you can obtain it as an array. This allows you to reset the processing, or requery.

Not sure I under that aducom.
But I prefer sc_lookup
sc_lookup(rs, $sql);
for($x=0 ; $x < count($rs) ; $x++){
$datetime = $rs[$x][1];
echo $datetime . “
” ;
}

@nico this is the array way. Nothing wrong with that, but does not work on large resultsets (will hang the system due to memory issues).

thank you for the feedback @aducom and @Nico

unfortunately it is a large resultset so Ill have to think of a different solution

Of course, you are correct. Forgot about large files
:slight_smile:

Then you need to perform the query twice. Some PDO drivers support bidirectional cursors, but tbh, I haven’t seen a good implementation yet.

A novices reply, but it would not work if you did it again, with a variable other than $rs

It is not logical to do so as I would make a for loop around it. But as you execute the full sql again, you could use a different var if you wanted to. But then the loop should contain the same new variable of course.