Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
您可以使用内置的 php 函数 json_encode() http://php.net/manual /en/function.json-encode.php
要对结果进行编码,请使用类似的内容
<?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);
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
使用 fetchAll() 方法PDOStatement 检索值的数组,然后将其传递给 json_encode()。
fetchAll()
json_encode()
$resultJSON = json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
Use the fetchAll() method of the PDOStatement to retrieve an array of the values, and then pass that to json_encode().
$array = $statement->fetchAll( PDO::FETCH_ASSOC ); $json = json_encode( $array );
我还发现在发回 json_encode() 返回的 JSON 对象之前插入和设置 PHP 标头('Content-Type: application/json')非常有用
I have also found it very useful to insert and set the PHP header('Content-Type: application/json') prior to sending back the JSON object returned by json_encode()
试试这个可能会对你有帮助
$data = array(); if($stmt->execute()){ while ($row = $stmt->fetchAll(PDO::FETCH_ASSOC)) { $data['data'] = $row; } } } if(!empty($data)){ header("Access-Control-Allow-Origin: *");//this allows cors header('Content-Type: application/json'); print json_encode($data); }else{ echo 'error'; }
Try this may be it's help you
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(5)
您可以使用内置的 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
使用
fetchAll()
方法PDOStatement 检索值的数组,然后将其传递给json_encode()
。Use the
fetchAll()
method of the PDOStatement to retrieve an array of the values, and then pass that tojson_encode()
.我还发现在发回 json_encode() 返回的 JSON 对象之前插入和设置 PHP 标头('Content-Type: application/json')非常有用
I have also found it very useful to insert and set the PHP header('Content-Type: application/json') prior to sending back the JSON object returned by json_encode()
试试这个可能会对你有帮助
Try this may be it's help you