PDO 语句转 JSON [英] PDOStatement to JSON

查看:20
本文介绍了PDO 语句转 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 PDOStatement 转换为 JSON?

How would I convert a PDOStatement to JSON?

我需要对 PDO::FETCH_OBJ 进行 jsonify.

I need to jsonify a PDO::FETCH_OBJ.

json_encode 无法对 PDO::FETCH_OBJ 进行 jsonify.

json_encode does not have the ability to jsonify a PDO::FETCH_OBJ.

推荐答案

您可以使用内置的 php 函数 json_encode() http://php.net/manual/en/function.json-encode.php

You can use the inbuilt php function json_encode() http://php.net/manual/en/function.json-encode.php

要对结果进行编码,请使用类似

To encode the results use something like

<?php
$pdo = new PDO("mysql:dbname=database;host=127.0.0.1", "user", "password");
$statement = $pdo->prepare("SELECT * FROM table");
$statement->execute();
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
$json = json_encode($results);

这篇关于PDO 语句转 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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