gpt4 book ai didi

php - 停止从 CSV 插入第一行

转载 作者:太空宇宙 更新时间:2023-11-03 12:12:09 24 4
gpt4 key购买 nike

我目前正在从 CSV 导入,但我似乎无法弄清楚如何停止插入第一行。

if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database

do {
if ($data[0]) {
mysql_query("INSERT INTO TABLE (id,type) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."'
)");
}
}
while ($data = fgetcsv($handle,0,",",'"'));

echo "Done";
}

任何帮助都会很棒!

最佳答案

如果您只想跳过第一个,然后放下 do 并将您的循环变成常规的 while 并确保调用 fgetcsv( ) 在循环之前一次。

if ($_FILES[csv][size] > 0) {
//get the csv file
$file = $_FILES[csv][tmp_name];
$handle = fopen($file,"r");
//loop through the csv file and insert into database

//Get some data first, and do nothing with it.
$data = fgetcsv($handle,0,",",'"');

while ($data = fgetcsv($handle,0,",",'"')) {
if ($data[0]) {
mysql_query("INSERT INTO TABLE (id,type) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."'
)");
}
}

echo "Done";
}

关于php - 停止从 CSV 插入第一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23815348/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com