user767556
user767556

Reputation:

Inserting several hundred records into mysql database using mysqli & PHP

I'm making a Flex 4 application and using ZendAMF to interact with a MySQL database. I got Flex to generate most of the services code for me (which utilizes mysqli) and roughly edited some of the code (I'm very much a novice when it comes to PHP). All works fine at this point.

My problem is - currently the application inserts ~400 records into the database when a user is created (it's saving their own data for them to load at a later date) but it does this with separate calls to the server - i.e. each record is sent to the server, then saved to the database and then the next one is sent.

This worked fine in my local environment, but since going on a live webserver it only adds these records some of the times. Other times it will totally ignore it. I'm assuming it's doing this because the live database doesn't like getting spammed with hundred of requests at practically the same time

I'm thinking a more efficient way would be to package all of these records into an array, send that to the server just the once and then get the PHP service to do multiple inserts on each item in the array. The problem is, I'm not sure how to go about coding this in PHP using mysqli statements.

Any help or advice would be greatly appreciated - thanks!

Upvotes: 1

Views: 332

Answers (2)

borrel
borrel

Reputation: 922

ee you should handle the user defaults separate and only store the changes

and if they are not saved you must check warnings form mysql or errors form php, data don't disappear

try

error_reporting(-1);

just before insering and

mysqli::get_warnings()

aferwards

Upvotes: 1

Pete Wilson
Pete Wilson

Reputation: 8694

Read up on LOAD DATA LOCAL INFILE. It seems it's just what you need for inserting multiple records: it inserts many records from a file (though not an array, unfortunately) into your table in one operation.

It's also much, much faster to do multiple-record UPDATEs with LOAD DATA LOCAL INFILE than with one-per-row UPDATEs.

Upvotes: 1

Related Questions