从我的数据库中,我只得到一个结果。尽管如此,我还是因为这段代码而返回了一个数组
while($obj = $stmt->fetch(PDO::FETCH_OBJ)){
$result[] = $obj;
}
返回的数组在索引 0 处只有一个元素(对象)。我想从这个对象访问一个属性。
我的代码如下所示:
return $result[0]->_id;
我收到通知
试图在...中获取非对象的属性
这是我的转储:
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
)
)
这肯定只是一个小错误,但我现在无法弄清楚。
编辑: 这是完整的功能:
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;
}
输出:
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
注意:尝试在第 4507 行的 /home/www/home/includes/class.DatabaseQuery.php 中获取非对象的属性