php数组插入mysql,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

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

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

标签:php,arrays,mysql,insert

来源: https://codeday.me/bug/20191003/1848895.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值