簡體   English   中英

將MySQL數據編碼為JSON

[英]Encoding MySQL data to 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);

我終於弄明白了。 因為我的$ 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'])];

否則,如果我寫

$array[] = utf8_encode($row)

我會給我一個錯誤。 我希望這可以幫助其他人解決這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM