简体   繁体   English

将MySQL数据编码为JSON

[英]Encoding MySQL data to JSON

Can anyone tell me why this is not working. 谁能告诉我为什么这不起作用。 I think I have literally looked at every video and webpage that talks about this and still all I get is a blank page and I have tried so many different ways. 我想我从字面上看过谈论这个问题的每个视频和网页,但我得到的只是空白页面,我尝试了许多不同的方法。 Here is my code. 这是我的代码。 The screen is just blank when it's echoed. 回显时,屏幕只是空白。 I am using PHP 5.6 我正在使用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);

If you want to see the results the site is http://bit.ly/1iAMnot 如果要查看结果,该站点为http://bit.ly/1iAMnot

Here is what the print_r says. 这是print_r所说的。 I'm only going to put one array because there are multiple. 我将只放入一个数组,因为有多个数组。

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.
)

Try this: 尝试这个:

$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);

I finally figured it out. 我终于弄明白了。 Because my $row['content'] contained UTF-8 characters, I had to put utf8_encode($row['content']) and write out the array like this 因为我的$ row ['content']包含UTF-8字符,所以我不得不放入utf8_encode($ row ['content'])并像这样写出数组

$array[] = ['id' => $row['id'], 'title'=> $row['title'], 'date' => $row['date'], 'header' => $row['header'], 'content'=> utf8_encode($row['content'])];

Otherwise if I wrote 否则,如果我写

$array[] = utf8_encode($row)

I would give me an error. 我会给我一个错误。 I hope this can help anyone else with this question. 我希望这可以帮助其他人解决这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM