【问题标题】:Undefined Offset :1 and 2未定义的偏移量:1 和 2
【发布时间】:2014-04-19 10:24:18
【问题描述】:

我正在使用explode函数在sql表中插入一个文本管道分隔的文件。

错误是:

Notice: Undefined offset: 1
Notice: Undefined offset: 2 
Both on line 34

即。

 $list=explode("|",$line);
 $sql="INSERT INTO tb (tb1,tb2,tb3) VALUES('$list[0]','$list[1]','$list[2]')";

请帮忙。

  while (!feof($handler)) // Loop til end of file.
  {
       $line= fgets($handler);     // Read a line
       $list=explode("|",$line);
       $sql="INSERT INTO tb (tb1,tb2,tb3) VALUES('$list[0]','$list[1]','$list[2]')";
       $result= mysql_query($sql,$conn);             
  }

【问题讨论】:

  • 您是否print_r($list) 确认有内容? undefined offset 表示没有$list[1]$list[2]
  • 是的,你是对的!没有 $list[1] 和 $list[2]。但是如何将炸开的 $line 插入到 sql 数据库中呢?

标签: php sql undefined offset explode


【解决方案1】:

如果你不关心价值观,你可以这样做

$list=explode("|", $line) + array('', '', '');

你的数组总是有 3 个元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-08
    • 2022-01-02
    • 2011-07-22
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多