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
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 ofNULL.If that’s not the case, then line 4507 points to somewhere else.