我在codeigniter中编写了以下代码。
foreach ($data as $value) {
# item_transactions fields and values
$data = array (
'checklist_item_id' => ($this->set['table'] == 'checklist_item') ? $this->set['id'] : null,
'imp_sequence_no' => $value['Sequence No.'],
'imp_vendor_tin' => $value['Vendor TIN'],
'imp_vendor_name' => $value['Vendor Name'],
'imp_input_vat_per_client' => floatval(str_replace(',', '', trim($value['Input VAT per client']))),
'imp_gsi' => $value['Goods/Services/Importations']
);
# Insert to item_transactions table
$id = parent::insert($data);
$count++;
}
return $count;
此代码的作用是从导入的csv文件中将数千条记录插入数据库表。
$data
是要插入的字段和值的关联数组。
问题
当我尝试导入包含4,000多条记录的csv文件时,我收到一条PHP错误Maximum execution time of 30 seconds exceeded.
我不想更改php.ini设置,因为大多数情况下您无法控制实时服务器设置。
有人能提出解决此问题的最佳方法吗?
答案 0 :(得分:1)
更改您的php.ini文件设置。 max_execution_time = 300
就够了。重启你的服务器。
答案 1 :(得分:1)
Codeigniter有一个支持批量上传的查询构建器类。您可以将数组或对象传递给函数。以下是使用数组的示例:
$data = array(
array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
),
array(
'title' => 'Another title',
'name' => 'Another Name',
'date' => 'Another date'
)
);
$this->db->insert_batch('mytable', $data);
Producess:
INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')