How to reuse the result of a sql query in PHP?

I would like to do different operations on data returned from one 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 it doesn't work. The first loop receives data, the second does not.

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

+2


source to share


2 answers


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



+3


source


Maybe.

The reason the second loop breaks is because every time you retrieve a row from the dataset, it shrinks until there are no more rows in the object.



You will need to make 2 copies of $ res and then run the second one while()

in the second $ res.

0


source







All Articles