【发布时间】:2014-11-23 11:15:04
【问题描述】:
我在PDO 中使用fetchAll() 方法发出问题。
当执行结果显示对象数组时它很好,但是每个对象都有重复的键,例如:
$catid = intval( $api->param['catid'] );
$json = array();
$prepar = "SELECT * FROM " . DB_PREFIX . "cat_sub WHERE `catid` = :catid ORDER BY `orderid` asc";
try{
$q = $api->pdo->prepare($prepar);
$q->bindparam(":catid" , $catid , PDO::PARAM_INT);
$q->execute();
$json = $q->fetchAll();
}catch( PDOException $e ){
$api->showError = $e->getMessage();
}
echo json_encode($json);
exit();
每个对象的输出是
{
"subcatid":"6",
"0":"6",
"title":"coool ",
"1":"coool ",
"catid":"2",
"2":"2",
"orderid":"1",
"3":"1"
},
它应该是
{
"subcatid":"6",
"title":"coool ",
"catid":"2",
"orderid":"1",
},
关于如何在没有循环或 foreach 的情况下执行此操作的任何建议 :)
【问题讨论】: