PHP分批处理MYSQL数据导入
2024-11-23 18:46:37
public function import_excel($id){ $id = $this->input->get('id'); $file_name = dr_get_file($id); $f = fopen($file_name, 'r'); $columns = fgetcsv($f); $rows[] = $columns; $_insert_func = function($data) { $this->db->insert_batch('imt_1_mall', $data); }; while ($row2 = fgetcsv($f)) { $row = array_combine($columns, $row2); $row2 = [ 'title' => trim(iconv('GBK','utf-8',$row['title'])), 'thumb' => '', 'keywords'=>'', 'description'=>trim(iconv('GBK', 'UTF-8', $row['description'])), 'contents'=>trim(iconv('GBK', 'UTF-8', $row['content'])), 'hits'=>'', 'uid'=>1, 'catid' =>dr_trans_catid(trim(iconv('GBK','utf-8',$row['erjifenlei']))), 'author' => 'mutaoinc', 'status'=>'9', 'url'=>'', 'link_id'=>'', 'tableid'=>'', 'inputip'=>$this->input->ip_address(), 'inputtime'=>SYS_TIME, 'updatetime'=>SYS_TIME, 'comments'=>'', 'favorites' => 0, 'displayorder' => 0, 'order_price'=>trim(iconv('GBK','utf-8',$row['order_price'])), 'order_quantity'=>trim(iconv('GBK','utf-8',$row['order_quantity'])), 'order_volume'=>trim(iconv('GBK','utf-8',$row['order_volume'])), 'order_specification'=>'', 'order_shipping'=>'', 'order_shipping_param'=>'', 'order_city'=>'', 'order_status'=>'', 'order_sn'=>trim(iconv('GBK','utf-8',$row['order_sn'])), 'cpbh'=>trim(iconv('GBK','utf-8',$row['cpbh'])), 'biaozhunzhi'=>trim(iconv('GBK','utf-8',$row['biaozhunzhi'])), 'cashao'=>trim(iconv('GBK','utf-8',$row['cashao'])), 'guige'=>trim(iconv('GBK','utf-8',$row['guige'])), 'huoqi'=>trim(iconv('GBK','utf-8',$row['huoqi'])), 'youxiaoqi'=>trim(iconv('GBK','utf-8',$row['youxiaoqi'])), 'yanzhidanwei'=>trim(iconv('GBK','utf-8',$row['yanzhidanwei'])), 'biaozhunjia'=>trim(iconv('GBK','utf-8',$row['biaozhunjia'])), 'zhuanshujia'=>trim(iconv('GBK','utf-8',$row['zhuanshujia'])), 'goods_ename'=>trim(iconv('GBK','utf-8',$row['goods_ename'])), 'picihao'=>trim(iconv('GBK','utf-8',$row['picihao'])), 'pinpai'=>trim(iconv('GBK','utf-8',$row['pinpai'])), 'jinjia'=>trim(iconv('GBK','utf-8',$row['jinjia'])), 'zhiliangnongdu'=>trim(iconv('GBK','utf-8',$row['zhiliangnongdu'])), 'staff_remark'=>trim(iconv('GBK','utf-8',$row['staff_remark'])) ]; $rows[] = $row2; //满一百条执行一次 if (count($rows) >= 100) { unset($rows[0]); $_insert_func($rows); $rows =[]; $rows[0] = $columns; } } //剩余不满100条执行 if ($rows){ unset($rows[0]); $_insert_func($rows); } }