mysql - PHP Split CSV file and update to Database doesn't work -
i split large csv file (20 000 lines) able put in mysql database (after use cronjob load php file every hours).
so have found code split file :
$inputfile = 'shipping.csv'; $outputfile = 'output'; $splitsize = 1000; $in = fopen($inputfile, 'r'); $rowcount = 0; $filecount = 1; while (!feof($in)) { if (($rowcount % $splitsize) == 0) { if ($rowcount > 0) { fclose($out); } $out = fopen($outputfile . $filecount++ . '.csv', 'w'); } $data = fgetcsv($in); if ($data) fputcsv($out, $data); $rowcount++; } fclose($out);
i have tried don't work :
require_once dirname(__dir__).'/pdo.php'; $inputfile = 'shipping.csv'; $outputfile = 'output'; $splitsize = 1000; //open uploaded csv file read mode $in = fopen($inputfile, 'r'); //skip first line fgetcsv($in); $rowcount = 0; $filecount = 1; while (!feof($in)) { if (($rowcount % $splitsize) == 0) { if ($rowcount > 0) { fclose($out); } $out = fopen($outputfile . $filecount++ . '.csv', 'w'); } $data = fgetcsv($in); if ($data){ fputcsv($out, $data); } while(($line = fgetcsv($out)) !== false){ //check whether member exists in database same email $prevquery = "select id shipbp license = '".$line[1]."'"; $prevresult = $bdd->query($prevquery); if($prevresult->rowcount() > 0) { $bdd->query("update shipbp set license = '".$line[0]."'"); } else{ $bdd->query("insert shipbp (license) values ('".$line[0]."')"); } } $rowcount++; } fclose($out);
csv file :
p135460,002,00003,250,as44563,0.35,,blabla,17/3/2017 00:00:00,sb,blabla
how can ?
thank help
Comments
Post a Comment