0

I have a .xls file which contains large number of rows, which I have to store to a DB. Now I'm reading each row in a loop and inserting into table. Is there any way, I can dump all rows to the table in a single shot? (with no loops?)

for ($row = 2; $row <= $highestRow; $row++) 
{ 

    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
    $entityName =$headings[0];
    $entityValue=$rowData[0];

    $rowData[0] = array_combine($headings[0], $rowData[0]);
    $num = count($rowData[0]);                                
    $excelDOB=$rowData[0]['date of birth'];
    $student = array(
                        'name'    =>  $name, 
                    );

    $this->common_model->insert_student_data($student);

}
2
  • I'm not a CI person, but a quick search gives stackoverflow.com/questions/17875706/…, if it works for you then this can be closed as a duplicate. Commented Jul 8, 2019 at 6:02
  • Even if you find a method that does this in one go, internally it will still loop over each row. This is not the issue. The problem is that executing an insert query for each row is slow. Inserting multiple rows in one INSERT query is much faster in MySQL. Commented Jul 8, 2019 at 6:37

2 Answers 2

0

You are using PHPExcel's rangeToarray() to get each single row. The range can be used by not only single row but also multiple rows.

$rowData = $sheet->rangeToArray('A1' . ':' . $highestColumn . $highestRow,NULL,TRUE,FALSE);

It means same

A1:J1
A2:J2
.
.
.
A1:J50

=>

A1:J50
Sign up to request clarification or add additional context in comments.

2 Comments

BUT STILL inserting this data to DB will need 'for loop'. this cannot be at once, only performs by row
As I said, inserting multiple data to DB cannot performs without loop
0

Instead of executing the query in Loops, You build Insert statements using loops and once loop is completed. Execute the query once. That way instead of executing 100/200/n number of queries all inserts will happen in 1 single query, Performance will be better.

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.