Re: Simple MySQL sample code runs out of memory

From: Date: Fri, 28 Oct 2011 02:50:14 +0000
Subject: Re: Simple MySQL sample code runs out of memory
References: 1 2 3 4  Groups: php.db 
Request: Send a blank email to php-db+get-48068@lists.php.net to get a copy of this message
On Thu, Oct 27, 2011 at 9:36 PM, <php@umpquanet.com> wrote: > On Thu, Oct 27, 2011 at 09:17:21PM -0500, tamouse mailing lists wrote: >> >> That said, I think there must be a way to do this in SQL. > > Absolutely, there's a way to do this in SQL; > > "select contract, sum(1) from test_table group by contract" > > > But as I said, simply counting is not the intent of the actual > processing that will be done. > > I need to find a method that can iterate through a large number > of records and process each record (in whatever way I choose to > define) without running out of RAM.  I only need to access one > record at a time, so I thought this would be pretty trivial, > and was surprised to find that the routine ran out of memory. > > This simple counting exercise is merely a test of the loop > algorithm so that I can do a 'cmp' or 'diff' comparison of the > output of the PHP versus the output of the SQL to verify that in > fact the PHP algorithm is indeed finding and processing each > record (and thus arriving at the same per-contract counts that > the SQL statement does). > > I will experiment with your suggestions. > > Thank you, > > Jim > > Ah, okay, did not get that -- sorry. Still, an inner and outer loop don't really seem necessary here, as you're spinning through one set of data, there doesn't seem a need to run through it that way. A more traditional method is to do something like: $last_contract = ''; while ($row = $msql_fetch_assoc($results) { if ($last_contract == $row['contract]) { // do what ever you'd do with the same contract } else { $last_contract = $row['contract']; // do what ever you'd do with the new contract } } assuming you are ordering by contract as in the example select.

« previous php.db (#48068) next »