sql PDO fetchall 重复的对象键? [英] sql PDO fetchall duplicate object keys?

查看:41
本文介绍了sql PDO fetchall 重复的对象键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 fetchAll() 方法在 PDO 中发出问题.

I've issue in PDO using fetchAll() method .

当执行显示对象数组的结果时,它很好,但每个对象都有重复的键,例如:

when execute the results showing array of objects its fine , but each object has duplicate keys for example :

$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 的情况下执行此操作的任何建议 :)

any advice how to do this without loop or foreach :)

推荐答案

你必须指定 获取模式

$q->fetchAll(PDO::FETCH_ASSOC);

这篇关于sql PDO fetchall 重复的对象键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆