public static function insertTable($table,$newArr){
#循环拼接sql 插入表中
try{
$str = '';
$itemKey= array_keys($newArr[0]);
$a ='';
$i = 0;
foreach ($newArr as $key =>$value){
$sqlKey = '';
foreach ($itemKey as $item =>$itemValue){
$sqlKey .= "{$itemValue},"; #拼接键
$value[$itemValue] = empty($value[$itemValue])?'':addslashes($value[$itemValue]);
$a .= "'{$value[$itemValue]}',";
}
$i ++;
$a = trim($a,',');
$str .= "({$a}),";
$a ='';
$sqlKey = "(".trim($sqlKey,',').")";
if($i % 1000 == 0){
$str = trim($str,',');
$sql = " replace into bq_hub.$table {$sqlKey} values ".$str;
DB::insert($sql);
$str = '';
}
if(count($newArr) == $i && !empty($str)){
$str = trim($str,',');
$sql = " replace into bq_hub.$table {$sqlKey} values ".$str;
DB::insert($sql);
}
}
return true;
}catch (\Exception $e){
return false;
}
PHP通过数组插入数据表
最新推荐文章于 2021-04-13 06:49:02 发布