【问题标题】:CSV File Upload Ignore first line and insert ony a few rowsCSV 文件上传 忽略第一行,只插入几行
【发布时间】:2014-09-15 14:55:45
【问题描述】:

我的代码没有问题,我只需要澄清一下:

我已经完成了忽略第一行的文件上传 CSV File Upload

PROBLEM:
After ignoring the first line i want only like 25 rows inserted;
What do i do?

Is this correct:
array fgetcsv ( resource $handle [, int $length [, string $delimiter [, string $enclosure [, string $escape]]]] ); 

can i use my $length?

【问题讨论】:

  • 当您到达第 26 行时,只需 break 您的循环(因为首先是标题)。 $length 用于优化行阅读。
  • 任何打破循环的示例代码

标签: php mysql csv


【解决方案1】:

只需使用 for 循环并忽略第一个条目。

for ($i=0; $i<=25; $i++){
   $line = fgetcsv($fp);
   if ($i==0){
      continue;
   }

   insert_into_db(line);
}

【讨论】:

  • 我还没有尝试过你的循环,但我相信它应该可以工作——感谢你的时间
【解决方案2】:

您也可以使用加载 INFILE 语句。

LOAD DATA INFILE '/tmp/test.txt' INTO TABLE test IGNORE 1 LINES; 

LOAD DATA INFILE 语句以非常高的速度将文本文件中的行读取到表中。

【讨论】:

  • 是的,没关系,但不是我想要的;我需要限制插入/上传的行,例如在 csv 中的 1000 个文件中只应插入 400 个。这就是我的意思
猜你喜欢
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-05
  • 1970-01-01
相关资源
最近更新 更多