-1

I need to loop through the results of a query a few times. After the first loop, I cannot go through the results again as the pointer is at the bottom. How can I make it return to the top? I dont want to run the query again as that is just going to use up more resources and return the same results.

$stmt = $conn->prepare("SELECT * FROM customers");
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();

//This kind of loop will repeat elsewhere in the code
while($row = $stmt->fetch()){ 
//Do something here
}
Reactgular
  • 52,335
  • 19
  • 158
  • 208
M9A
  • 3,168
  • 14
  • 51
  • 79

1 Answers1

2
while($row = $stmt->fetch()){ 
    $rows[] = $row;  //use $rows later
    //Do something here with $row
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87