views:

254

answers:

2

I'd like to do different operations on the data returned from a single sql query, something like this:

$sql = "SELECT * FROM mandant";  
$res = pg_query($_db,$sql); 

while ($mandant_list = pg_fetch_object($res)) 
{          
  # do stuff
}

while ($mandant = pg_fetch_object($res))
{
  # do other stuff
}

But this doesn't work. The first loop gets the data, the second one not.

Is it possible to reuse the result returned from the query without running it again? If not, why? What happens to the $res variable?

A: 

It is possible.

The reason the second loop breaks is because every time you are fetching a row from the data set it is being decremented until there are no rows left in the object.

You'll need to make 2 copies of $res and then run the second while() on the second $res.

Evernoob
+1  A: 

Yes, you can "rewind" the result resource by using pg_result_seek().

VolkerK