-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 } 
0

1 Answer 1

2
while($row = $stmt->fetch()){ $rows[] = $row; //use $rows later //Do something here with $row } 
Sign up to request clarification or add additional context in comments.

1 Comment

Just saw duplicate. Just use $rows = $stmt->fetchAll();

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.