【发布时间】:2020-02-07 06:12:55
【问题描述】:
我试图在 mysql 表中插入数组...但我的代码不起作用
$File = 'testfile.csv';
$arrResult = array();
$handle = fopen($File, "r");
$row = 0;
if(empty($handle) === false) {
while(($data = fgetcsv($handle, 1000, ";")) !== FALSE){
$arrResult[] = $data;
$num = count($data); //2100 resultats in my testfile
$row++;
if($row>1){ //ignore header line
for ($c=0; $c < $num; $c++) { //start loop
$sql = '
INSERT INTO MyTable (name, class, level, ability)
VALUES ("'.$data[0].'","'.$data[1].'","'.$data[2].'","'.$data[3].'")
';
$Add=$db->query($sql);
}
}
}
fclose($handle);
};
Mytable 中的结果:
1、Hero1、战士、65、活力;
2、Hero1、战士、65、活力;
3、Hero1、战士、65、活力;
4、Hero1、战士、65、活力;
...
【问题讨论】:
-
这个结果有什么不正确的地方?你期待什么结果?
-
为此使用准备好的语句。
-
摆脱你的
for循环,fgetcsv已经一次得到一行 -
@AlexBarker 这里是 testfile 的内容:Hero1;战士; 65;活力;英雄2;牧师; 32;魔法;英雄3;忍者; 78;逃脱;英雄4;圣骑士; 21;愤怒;