我有一个烦人的问题,无论我告诉PDO
返回一个数组或对象,它总是将它放在另一个数组中。IE。:
Array (
[0] => stdClass Object ( [name] => Test Topper [urlName] => test-topper )
[1] => stdClass Object ( [name] => Test Topper [urlName] => test-topper )
[2] => stdClass Object ( [name] => adfg dfg [urlName] => adfg-dfg )
[3] => stdClass Object ( [name] => adfg dfg [urlName] => adfg-dfg )
)
我该如何停止这种行为,所以我最终得到:
stdClass Object ( [name] => Test Topper [urlName] => test-topper )
[1] => stdClass Object ( [name] => Test Topper [urlName] => test-topper )
[2] => stdClass Object ( [name] => adfg dfg [urlName] => adfg-dfg )
[3] => stdClass Object ( [name] => adfg dfg [urlName] => adfg-dfg )
所以我可以使用$data->name
而不是访问属性$data[0]->name
?
这是负责获取数据的 PHP:
public function fetchToppersAction($type, $orderBy, $limit)
{
$PDOParams = array() ;
$SQL = 'SELECT name, urlName FROM toppers_images, toppers
LEFT JOIN meta_descriptions AS md ON md.topperId = toppers.id
' ;
if($type)
{
$SQL .= 'WHERE typeId = ?' ;
array_push($PDOParams, $type) ;
}
$SQL .= 'ORDER BY ? LIMIT ?' ;
array_push($PDOParams, $orderBy, $limit) ;
$r = $this->db->prepare($SQL) ;
$r->execute($PDOParams) ;
return $r->fetchAll(PDO::FETCH_OBJ) ;
}