我有一个包含30000个条目的数组,需要进入MySQL表.
什么是最佳做法?从这里?让我们说数据库中的[0],[1]和[2]将是’title’,’type’和’customer’
它是否添加了与表中列名匹配的键名并调用som“magic”函数?或手动构建查询…
Array
(
[0] => Array
(
[0] => 2140395946
[1] => 1SAP
[2] => 0041451463
)
[1] => Array
(
[0] => 2140411607
[1] => 2SAP
[2] => 0041411940
)
[2] => Array
(
[0] => 2140706194
[1] => 4SAP
[2] => 0041411943
)
etc. etc.
更新 – 基于答案
谢谢你的回答.
解决方案通常是手动创建SQL字符串,所有行都可以作为一个插入:
INSERT INTO `tx_opengate_table` (`machine` ,`customer` ,`type`)
VALUES
('m123', 'dfkj45', 'A'),
('m137', 'kfkj49', 'A'), "repeat this line for each entry in the array"
... ... ...
('m654321', '34dgf456', 'C4') "end with out comma, or remove last comma"
;
TYPO3特别
我碰巧在CMS TYPO3中这样做了,刚刚遇到一个不久前添加的新功能:
//Insert new rows
$table = 'tx_opengate_stuff';
$fields = array('machine','type','customer');
$lines = "array as given above"
$GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows($table,$fields,$lines);
解决方法:
我会说你自己建造它.您可以这样设置:
$query = "INSERT INTO x (a,b,c) VALUES ";
foreach ($arr as $item) {
$query .= "('".$item[0]."','".$item[1]."','".$item[2]."'),";
}
$query = rtrim($query,",");//remove the extra comma
//execute query
如果有必要,不要忘记逃避报价.
另外,请注意不要一次发送太多数据.您可能必须以块的形式执行它而不是一次性执行它.
标签:php,arrays,mysql,insert
来源: https://codeday.me/bug/20191003/1848895.html