PHP PDO读取MariaDB数据转换为json

5 篇文章 0 订阅

pdo读取的数据

pdo查询返回的是对象,直接输出query是无法解析的。。只会一行一行输出
$query = c o n n − > q u e r y ( conn->query( conn>query(sql)->fetchALL()返回的是数组,直接输出就是Array

$conn = new PDO("mysql:host=$dbhost;dbname=$dbname;charset=$charset", $dbuser, $dbpass);
 $sql = "SELECT * FROM $tbname";
 if ( $query = $conn->query($sql) )
 {
     echo "<p>编号  名称 开始时间 结束时间</p>";
     foreach ($query as $row)
     {
         echo $row['activity_no'] .'      ' ;
         echo $row['activity_name'] .'      ' ;
         echo $row['activity_begin_time'] .'      ' ;
         echo $row['activity_end_time'] .'      ' ;
         echo $row['activity_location'] ;
         echo $row['activity_content'] . "<br>";
     }
   }
编号 名称 开始时间 结束时间

1 雷雨 2020-06-12 20:59:56 2020-06-15 20:59:58
2 头脑风暴 2020-06-12 21:01:04 2020-06-13 21:01:05
3 瑕积分
4 诗歌朗诵
6 最炫民族风 北极尬舞

转换json

不知道为什么就想起了这个格式,前端都喜欢这个哇。。。。

if ( $query = $conn->query($sql) )
{
   $result = json_encode($query->fetchAll());
   echo $result;
}
[{"activity_no":"1","0":"1","activity_name":"\u96f7\u96e8","1":"\u96f7\u96e8","activity_begin_time":"2020-06-12 20:59:56","2":"2020-06-12 20:59:56","activity_end_time":"2020-06-15 20:59:58","3":"2020-06-15 20:59:58","activity_location":null,"4":null,"activity_content":null,"5":null},{"activity_no":"2","0":"2","activity_name":"\u5934\u8111\u98ce\u66b4","1":"\u5934\u8111\u98ce\u66b4","activity_begin_time":"2020-06-12 21:01:04","2":"2020-06-12 21:01:04","activity_end_time":"2020-06-13 21:01:05","3":"2020-06-13 21:01:05","activity_location":null,"4":null,"activity_content":null,"5":null},{"activity_no":"3","0":"3","activity_name":"\u7455\u79ef\u5206","1":"\u7455\u79ef\u5206","activity_begin_time":null,"2":null,"activity_end_time":null,"3":null,"activity_location":null,"4":null,"activity_content":null,"5":null},{"activity_no":"4","0":"4","activity_name":"\u8bd7\u6b4c\u6717\u8bf5","1":"\u8bd7\u6b4c\u6717\u8bf5","activity_begin_time":null,"2":null,"activity_end_time":null,"3":null,"activity_location":null,"4":null,"activity_content":null,"5":null},{"activity_no":"6","0":"6","activity_name":"\u6700\u70ab\u6c11\u65cf\u98ce","1":"\u6700\u70ab\u6c11\u65cf\u98ce","activity_begin_time":null,"2":null,"activity_end_time":null,"3":null,"activity_location":"\u5317\u6781","4":"\u5317\u6781","activity_content":"\u5c2c\u821e","5":"\u5c2c\u821e"}]

但是仔细一看不仅输出了默认的键-值对,还多出了默认的数字下标-值对,emm那肯定要把其中一个去掉哇,这里去掉后面的

if ( $query = $conn->query($sql) )
{
   $result = json_encode($query->fetchAll(PDO::FETCH_ASSOC));
   echo $result;
}
[{"activity_no":"1","activity_name":"\u96f7\u96e8","activity_begin_time":"2020-06-12 20:59:56","activity_end_time":"2020-06-15 20:59:58","activity_location":null,"activity_content":null},{"activity_no":"2","activity_name":"\u5934\u8111\u98ce\u66b4","activity_begin_time":"2020-06-12 21:01:04","activity_end_time":"2020-06-13 21:01:05","activity_location":null,"activity_content":null},{"activity_no":"3","activity_name":"\u7455\u79ef\u5206","activity_begin_time":null,"activity_end_time":null,"activity_location":null,"activity_content":null},{"activity_no":"4","activity_name":"\u8bd7\u6b4c\u6717\u8bf5","activity_begin_time":null,"activity_end_time":null,"activity_location":null,"activity_content":null},{"activity_no":"6","activity_name":"\u6700\u70ab\u6c11\u65cf\u98ce","activity_begin_time":null,"activity_end_time":null,"activity_location":"\u5317\u6781","activity_content":"\u5c2c\u821e"}]

参考

  • https://blog.csdn.net/weixin_30469895/article/details/95589933
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值