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 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?