Skip to content

Instantly share code, notes, and snippets.

@storyflow
Created March 22, 2018 08:43
Show Gist options
  • Save storyflow/dfc9470795d2cda2a6e1792e8aac50d7 to your computer and use it in GitHub Desktop.
Save storyflow/dfc9470795d2cda2a6e1792e8aac50d7 to your computer and use it in GitHub Desktop.
【Mysql】批量插入数据
function insert_batch($table, $data)
{
global $ms_import;
$keys = array_keys(current($data));
$values = [];
foreach ($data as $clean) {
foreach ($clean as $kk=>$vv) {
$clean[$kk] = '\'' . addslashes($vv) . '\'';
}
$values[] = '('.implode(',', $clean).')';
}
$sql = _insert_batch($table, $keys, $values);
$res = $ms_import->query($sql);
return $res;
}
function _insert_batch ($table, $keys, $values)
{
return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES ".implode(', ', $values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment