2015-09-11 67 views
2

任何人都可以告訴我爲什麼這不起作用。我想我已經從字面上看過所有關於這個問題的視頻和網頁,但我仍然只是一個空白頁面,而且我嘗試了很多不同的方式。這是我的代碼。回聲時屏幕只是空白。我使用PHP 5.6將MySQL數據編碼爲JSON

//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. 
) 
+0

當你做'print_r($ row);'?有沒有結果? – aldrin27

+0

@ aldrin27是的,它打印所有的陣列 –

+0

我可以看到輸出? – aldrin27

回答

0

我終於想通了。因爲我的$行[「內容」]包含UTF-8字符,我只好把函數utf8_encode($行[「內容」])與否則寫出這樣

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

的數組,如果我寫

$array[] = utf8_encode($row) 

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

+0

我想在你的頭文件中加入。將所有結果編碼爲utf8 – aldrin27

0

試試這個:

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

它仍然只是一個空白頁面。我不知道它是否可以與我的服務器在DreamHost –

+0

嘗試print_r($ array); – aldrin27

+0

這也打印出這個數組。我拿出數據保存室 '陣列 ( [0] =>數組 ( [ID] => [標題] => [日期] => [標題] => [內容] => )' –