微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php mysql:将一个php数组插入mysql

我有一个包含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

如果有必要,不要忘记逃避报价.

另外,请注意不要一次发送太多数据.您可能必须以块的形式执行它而不是一次性执行它.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐