dongling2545 2013-01-23 10:11
浏览 32
已采纳

对象数组:想要访问一个属性但我得到“试图获取非对象的属性...”

From my database I get only one result. Nevertheless I get an array returned because of this code

while($obj = $stmt->fetch(PDO::FETCH_OBJ)){
    $result[] = $obj;
}

The returned array has only one element (object) at the index 0. From this object I want access a property.

My code looks like:

return $result[0]->_id;

I get the notice

Trying to get property of non-object in ...

Here are my dumps:

var_dump

array(1) { [0]=> object(stdClass)#4 (13) { ["_id"]=> string(8) "43001613" ["another-property"]=> string(5) "20608" } }

print_r

Array
(
    [0] => stdClass Object
        (
            [_id] => 43001613
            [another-property] => 20608
        )

)

It is for sure only a small error but I can't figure it out now.

Edit: Here is the full function:

public function getOrtIDForPLZ($plz){
    $sql = "SELECT * FROM verortung WHERE plz = :plz LIMIT 1;";
    $result = $this->ExecuteQuery($sql, array("plz"=>$plz));

    var_dump($result);
    echo '<pre>';
    print_r($result);
    echo '</pre>';

    return $result[0]->_id;
}

Output:

array(1) { [0]=> object(stdClass)#32 (13) { ["_id"]=> string(8) "43006637" ["gemeindekennziffer"]=> string(5) "32330" ["gemeindename"]=> string(13) "Theresienfeld" ["ortsname"]=> string(13) "Theresienfeld" ["plz"]=> string(4) "2604" ["bezirk"]=> string(21) "Wiener Neustadt -Land" ["bundesland"]=> string(17) "Niederösterreich" ["lat"]=> string(11) "47.85000000" ["lon"]=> string(11) "16.23333330" ["einwohner_2001"]=> string(4) "2490" ["kfz-kennzeichen"]=> string(2) "WB" ["fläche_km2"]=> string(5) "11.44" ["seehöhe"]=> string(3) "282" } }

Array
(
    [0] => stdClass Object
        (
            [_id] => 43006637
            [gemeindekennziffer] => 32330
            [gemeindename] => Theresienfeld
            [ortsname] => Theresienfeld
            [plz] => 2604
            [bezirk] => Wiener Neustadt -Land
            [bundesland] => Niederösterreich
            [lat] => 47.85000000
            [lon] => 16.23333330
            [einwohner_2001] => 2490
            [kfz-kennzeichen] => WB
            [fläche_km2] => 11.44
            [seehöhe] => 282
        )

)

NULL

Notice: Trying to get property of non-object in /home/www/home/includes/class.DatabaseQuery.php on line 4507

  • 写回答

1条回答 默认 最新

  • dow66098 2013-01-23 10:28
    关注

    You're probably calling this method multiple times. In the first call everything is all right, but in the next one...

    See that NULL? Surely you cannot get a property of NULL.

    If that's not the case, then line 4507 points to somewhere else.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集