PHP列表问题(未定义偏移量:)

7

原始SQL查询如下:SELECT id、post_title、post_date FROM wp_posts WHERE id='1'

当我检索记录时,我能够找到它,但当涉及返回结果时,我感到困惑。这就是我卡住的地方。

   while ($row = mysql_fetch_assoc($RS)) :
      print_r ($row); 
      list($id,$post_title,$post_date) = $row;
   endwhile;

print_r ($row) 输出如下内容: Array ( [ID] => 1 [post_title] => 你好,世界! [post_date] => 2012-03-27 03:28:27 )

当我在其中运行列表函数时(显然是为了调试),我得到了这个结果;

Notice: Undefined offset: 2 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 1 in F:\inetpub\wwwroot\whatever\sql.php on line 147
Notice: Undefined offset: 0 in F:\inetpub\wwwroot\whatever\sql.php on line 147

这是什么原因引起的?
5个回答

6
我想答案就在这里: list() 只适用于数字数组,并假定数字索引从0开始。
:(

6

替换:

mysql_fetch_assoc($RS)

为:

mysql_fetch_array($RS, MYSQL_NUM)

这样就能够正常工作了,因为list函数尝试使用数字键访问数组。


2

1
你使用了 mysql_fetch_assoc 函数,因此每行结果数组的数据都是通过列名作为键来获取的,而“list”则试图使用数值数组索引将变量与值匹配。你可以改用 mysql_fetch_array 函数。

0

$categ = val1 | val2

list($one,$two,$three)=@split('[|]',$categ);

如果您尝试列出不可用的值,它将返回Undefined Offset错误。

这里的错误将是Undefined Offset 2。

因为在分割$categ时,它只有两个值,如果您尝试访问第三个值,则会返回错误。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接