为什么会出现“未定义的偏移量:0"? [英] Why do I get 'Undefined offset: 0'?

查看:70
本文介绍了为什么会出现“未定义的偏移量:0"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个网站,看起来不错. 然后,我打开调试以查看是否有任何错误,然后得到以下消息:

I'm develop a website and it looks fine. Then I turned on debugging to see if I had any errors, and I get this message:

Notice: Undefined offset: 0 in I:\path\to\file\MyFile.class.php on line 105

在该行上,您将找到以下代码:

On that line you will find the following code:

$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result[0]; // line 105

如果我执行print_r(result),则会得到以下结果:

If I do a print_r(result) I get the following result:

Array (
    [0] => Array (
        [id] => 3721
        [parentID] =>
    )
)
Array (
) 

如您所见,结果包含两个数组.第一个没有数据,第二个没有数据.我从未见过print_r这样输出两个数组.我猜这是第二个给我带来麻烦的问题.

As you can see the result contains twoarrays. The first one has data, the second one has no data. I've never seen print_r output two arrays like this. I'm guessing it's the second one causing me the problems.

有人能告诉我为什么第二个阵列会潜入吗?

Can anyone advice me of why that second array sneaks in?

VAR转储

array
  0 => 
    array
      'id' => string '3721' (length=4)
      'parentID' => null
array
  empty

推荐答案

您将提取与查询匹配的所有记录.如果没有记录,它将返回一个空数组.显然,空数组中没有0元素.

You fetch all records matching the query. If there are no records it will return an empty array. Obviously there is no 0 element in an empty array.

这篇关于为什么会出现“未定义的偏移量:0"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆