为什么会出现“未定义的偏移量:0"? [英] Why do I get 'Undefined offset: 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屋!