php - 将 MySQL 数据编码为 JSON

标签 php mysql json

谁能告诉我为什么这不起作用。我想我确实看过所有谈论这个的视频和网页,但我得到的仍然是一个空白页面,我尝试了很多不同的方法。这是我的代码。回显时屏幕只是空白。我正在使用 PHP 5.6

    //header('Content-Type: application/json');
include_once('../db.php');

$sql = "SELECT * FROM `blog` ORDER BY `id` ASC";


$result = mysqli_query($conn, $sql);
$array = array();
while($row = mysqli_fetch_array($result)) {
    $array[] = ['id' => $row['id'], 'title'=> $row['title'], 'date' => $row['date'], 'header' => $row['header'], 'content'=> $row['content']];
}
//print_r($array);
echo json_encode($array);
//print_r($array);
echo count($array);

mysqli_close($conn);

如果您想查看结果,网站是 http://bit.ly/1iAMnot

这是 print_r 所说的。我只打算放一个数组,因为有多个数组。

Array
(
    [0] => 1
    [id] => 1
    [1] => Explore More
    [title] => Explore More
    [2] => 2015-08-22 11:58:46
    [date] => 2015-08-22 11:58:46
    [3] => http://passionla.com/img/blog/explore-more.jpg
    [header] => http://passionla.com/img/blog/explore-more.jpg
    [4] => "For since the creation of the world God's invisible qualities--his eternal power and divine nature--have been clearly seen, being understood from what has been made, so that people are without excuse."
<br /><br />
- Romans 1:2
<br /><br />
The world around us is amazing. If you have recently seen a sunset or even just the wind blow through the leaves, causing them to rustle on the tree, you have witnessed just a piece of God's divine masterpiece that we can see in nature. 
<br /><br />
His beauty is all around you. Like the verse from romans says, His eternal power and divine nature are seen clearly in his creation. It is easy to get caught up in our busy lives and never look around.
<br /><br />
You don't have to go far to see god's magnificence. Take some time today to sit outside, to take a hike, or go for a walk with the intent of seeing God's majesty. Remember to take a second to look around and appreciate the glorious god that created you. 
<br /><br />
Explore more, He's created a big world out there.
    [content] => "For since the creation of the world God's invisible qualities--his eternal power and divine nature--have been clearly seen, being understood from what has been made, so that people are without excuse."
<br /><br />
- Romans 1:2
<br /><br />
The world around us is amazing. If you have recently seen a sunset or even just the wind blow through the leaves, causing them to rustle on the tree, you have witnessed just a piece of God's divine masterpiece that we can see in nature. 
<br /><br />
His beauty is all around you. Like the verse from romans says, His eternal power and divine nature are seen clearly in his creation. It is easy to get caught up in our busy lives and never look around.
<br /><br />
You don't have to go far to see god's magnificence. Take some time today to sit outside, to take a hike, or go for a walk with the intent of seeing God's majesty. Remember to take a second to look around and appreciate the glorious god that created you. 
<br /><br />
Explore more, He's created a big world out there.
)

最佳答案

试试这个:

$array = array();
while($row = mysqli_fetch_array($result)) {
    $array[] = ['id' => $row['id'], 'title'=> $row['title'], 'date' => $row['date'], 'header' => $row['header'], 'content'=> $row['content']];
}
echo json_encode($array);

关于php - 将 MySQL 数据编码为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32513482/

相关文章:

json - PostgreSQL - 从查询构造格式良好的 json 对象

PHP pthreads - 共享对象

php - 按 DATEPART() 选择行

php - yii mysql 通过查询生成器使用子查询

mysql - varchar 和 int 列之间的比较问题

arrays - 如何在 JSONB 类型列中重新排序数组

PHP 从 TextArea 中提取代码并传递到 Mysql SELECT IN 查询

php - MySQL 列计数与第 1 行的值计数不匹配

php - POST请求在android上根本不起作用

IE9 回调上 JSON 的 jQuery AJAX 请求未定义