我已經寫了一個php代碼來使mysql數據庫中的JSON文件。當我檢查了JSON文件,它會顯示一些數據NULL,雖然我的數據庫沒有NULL數據.. 這是我的PHP代碼:爲什麼JSON雖然數據庫中有數據,但顯示空數據?
$selectQuery = "SELECT tour_code, day, schedule_title, details FROM tbl_tour_details WHERE status=0";
$response = array();
$posts = array();
$result = mysql_query($selectQuery) or die ("Errored Query: ".$selectQuery);
if(mysql_num_rows($result))
{
while($row = mysql_fetch_assoc($result))
{
$tour_code = $row['tour_code'];
$day = $row['day'];
$schedule_title = $row['schedule_title'];
$details = $row['details'];
$posts[] = array('tour_code'=>$tour_code, 'day'=>$day, 'schedule_title'=>$schedule_title, 'details'=>$details);
}
}
$response['posts'] = $posts;
$fp = fopen('..//webservices//json//tour_details.json','w');
fwrite($fp, json_encode($response));
fclose($fp);
這裏,數據類型,且尺寸是:
tour_code = int
day = int
schedule_title = varchar(150)
details = text
我的數據是這樣的:
tour_code =1
day = 3
schedule_title = "Singapore – Genting Highland By Coach (350 KM 5 HRS)"
details = "This Morning Is The Day Of FUN And Adventure For The Family. Enjoy Spectular Rides And Shows At The Ever-Popular Genting Outdoor Theme Park With Its Myriad Attractions For The Young And Old Alike. Take An Exciting Drive In The Antique Car Or Daring Spins On The Roller Coaster, It’s A Magical Adventure Of Fun And Excitement For The Whole Family!
Over Night At Hotel First World Deluxe Or Theme Park Or Awana Resort Or Similar.
(Breakfast, Lunch & Dinner)"
這裏是我的JSON文件(包括如果爲null,/ R,/ N): json file
這裏是我的var_dump文件(截斷某些數據爲 「...」) var_dump file
這裏是我的print_r($posts)
文件(這是完全正確的): print_r($posts file
)
什麼是print_r($ posts)的值;後循環.. –
只需看看手冊,['json_decode'函數](http://php.net/json_decode)的返回值,包括錯誤情況以及如何獲得有關任何潛在的更多信息錯誤,在那裏解釋:http://php.net/json_decode – hakre
[**請不要在新代碼中使用'mysql_ *'函數**](http://stackoverflow.com/a/14110189/1723893 )。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 –