Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7883061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T04:26:04+00:00 2026-06-03T04:26:04+00:00

Assuming I have $db is an instance of Zend_Db_Adapter_Abstract and $sql = ‘SELECT blah

  • 0

Assuming I have

$db is an instance of Zend_Db_Adapter_Abstract and
$sql = 'SELECT blah blah FROM table' will return a huge number of records.

There are two code fragments to process the returned data as follows.

// Code fragment 1 (let's call it C1).
$results = $db->fetchAll($sql);
foreach ($results as $row) {
    // Process $row
}

// Code fragment 2 (let's call it C2).
$stmt = $db->query($sql);
while ($row = $stmt->fetch()) {
    // Process $row
}

My understanding is that C1 will load all returned data to $results. So, a huge data is loaded to PHP memory. Below are my questions.

  1. Does C2 load all data to PHP memory or does it process one by one like prepare/execute?
  2. Assuming there is no other option, is C1 or C2 a better option?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T04:26:05+00:00Added an answer on June 3, 2026 at 4:26 am

    Your hunch is correct. At least if you’re using the PDO driver, ->fetch() reads the results unbuffered, whereas ->fetchAll() returns all the data in a big array.

    Be aware that if you’re using ->fetch(), you have to be careful about what you try to do inside your loop. You can’t run additional queries on the same connection while you’ve still got an unbuffered result set.

    So, if your plan is to update those same rows inside the loop, you’ll need to find a way to delay executing the updates (by queuing then up somehow) until you’ve exited the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming I have a instance of class A (name it AI) that has an
For instance, if I have robots.txt declared as a static resource, and assuming that
Assuming I have an instance of an object that I know belongs to a
I have a large (multi-GB) data file exported from an Oracle table. I want
Assuming I have just used an XPath query to get the 5th instance of
Using SQL Server 2008: assuming I have a valid geography object geog1 , converting
any better way to do that (assuming I have 10 a elements, for instance)?
I have an SQL Server 2005 instance whose full backup (.BAK) failed due to
While on return to my app from getting phone number from address book, if
I have a WPF application with MVVM. Assuming object composition from the ViewModel down

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.