memory errors using foreach loops

I have a PHP file that uses the NuSOAP toolkit to connect to an API and return records that I then write to a local database.  I'm getting memory errors long before every record is returned.

The first problem - I'm not awesome at PHP.

I've read about foreach loops not being memory friendly and that I should be using for loops instead.  But (referencing 'the first problem'), I need a little help.

Anybody want to help me get this file to run without throwing memory errors (and maybe make it a little more efficient in the process)?

Carlos ElguetaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

One potential problem I see is that you are creating a new db connections for every loop in your foreach

Another thing to look at would be using prepared statements

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Carlos ElguetaAuthor Commented:
I've removed the db connections.  Will the statement preparations really help reduce memory?

One thing if tried is replacing

foreach ($response['data'] as $data)

Open in new window

$record = $response['data'];
foreach ($record as $data)

Open in new window

Because I had read it could improve memory when using foreach loops.
With the prepared statement the sql is sent to the sql server then in your loop you are just sending the data to use in the sql so it should be a lot faster and use less memory.

Your code above makes no difference really.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Carlos ElguetaAuthor Commented:
Ok - I'll give it a shot.  The code above was in reference to a blog re: a memory leak in PHP 5 foreach loops - just figured I'd try it.
How many records are you inserting here?
And remember using Frameworks is alway memory hungry
Carlos ElguetaAuthor Commented:
I've requested that this question be deleted for the following reason:

No longer need an answer...
Carlos ElguetaAuthor Commented:
Thanks for your help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.