0

I changed my xls file to csv format for write to MySql. Because I get 60 second timeout error when i tried to save fields from xls file to MySQL. Now when i write echo code i am getting all my data in just 5 seconds. But when i try to save it to MySQL 60 second is not enough. Here is my codes:

$csvfile = fopen("data.csv","r");
    fgetcsv($csvfile);
    while (($data = fgetcsv($csvfile))!==FALSE){
        $ekle = $dbpdo->prepare("INSERT INTO isletmeler (sno) VALUES ('".$data[0]."')");
        $ekle->execute();   
    }

What can i do to speed up the speed of saving data to MySql? I have 29 column in csv but i tried to save just 1 column for see if it save my all data. I have about 2700 fields in csv but it just saving about 1000 fields in 60 second.

1 Answers1

0

You could restructure the code so that the prepare is only done once...

$csvfile = fopen("data.csv","r");

fgetcsv($csvfile);
$ekle = $dbpdo->prepare("INSERT INTO isletmeler (sno) VALUES (?)");
while (($data = fgetcsv($csvfile))!==FALSE){
    $ekle->execute([$data[0]]);   
}

This reduces the need to re-prepare the statement each time.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55