0

I have a code to upload xlsx to Mysql tables and the load is done with chunks because the file is too large, so I have to INSERT every chunk. The for loop works good it has been tested with echo statements but the INSERT is only done in the first chunk. Any idea of how I can solve this? This is part of the code:

for ($startRow = 2; $startRow <= $totalRows; $startRow += $chunkSize) {
  $chunkFilter->setRows($startRow,$chunkSize);
  //Load of the data
  $objPHPExcel = $objReader->load($newFilePath);
  $objWorksheet4 = $objPHPExcel -> getSheetByName("MIFReportExport");
  //Getting data of every cell
  $highestRow4 = $objWorksheet4->getHighestRow();
  $highestColumn4 = $objWorksheet4->getHighestColumn();
  $highestColumnIndex4 = PHPExcel_Cell::columnIndexFromString($highestColumn4);
  $rows = array();
  for ($row = 2; $row <= $highestRow4; ++$row) {
     for ($col = 0; $col <= $highestColumnIndex4; ++$col) {
          $rows[$col] = $objWorksheet4->getCellByColumnAndRow($col, $row)->getCalculatedValue();
}
 $result = mysql_query("INSERT INTO fy16 (SerialNumber, BrandedModelName, GenericProductCode, Familia, ProductLineModelFile)VALUES ('".$rows[0]."', '".$rows[1]."', '".$rows[2]."', '".$rows[3]."', '".$rows[4]."', '".$rows[5]."')",$con) or die(mysql_error());

}

}

Thanks in advance.

3
  • 3
    Stop using mysql_*API. It is deprected. Commented Feb 3, 2017 at 12:25
  • check what values have on start and on end: $totalRows & $chunkSize Just add echo $totalRows.' '.$chunkSize; before for (&start...) and after end } Commented Feb 3, 2017 at 12:27
  • place your query inside the loop Commented Feb 3, 2017 at 12:51

1 Answer 1

1

First of all, you should take a look at PDO, and prepared statements. Here is how to create a MASS insert query

$valuesToInsert = [
    ["firstColumn" => "item1", "secondColumn" => "item1"],
    ["firstColumn" => "item2", "secondColumn" => "item2"],
    ["firstColumn" => "item3", "secondColumn" => "item3"],
];

$valuesTab = array_map(function ($tab) { return "({$tab['firstColumn']}, {$tab['secondColumn']})"; },$valuesToInsert);


$query = "INSERT INTO `tableName` (firstColumn, secondColumn) VALUES ". implode(',', $valuesTab);

Output :

INSERT INTO
  `tableName` (firstColumn, secondColumn)
VALUES
  (item1, item1),(item2, item2),(item3, item3)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.