Iam目前正在構建一個HTML5應用程序,該應用程序從php webservice獲取其數據json編碼。 我也有小圖像存儲在數據庫中,並希望將它們返回爲base64編碼的json結果。在php數組中使用base64編碼/替換blob
目前我得到的所有MySQL的字段,然後返回作爲JSON是這樣的:
$query = "SELECT * FROM `news` ";
$posts = array();
if(mysql_num_rows($result)) {
while($post = mysql_fetch_assoc($result)) {
$posts[] = array('post'=>$post);
}
}
header('Content-Type: text/javascript');
echo json_encode(array('posts'=>$posts));
我有一個名爲圖片字段。 現在我想用base64轉換數組posts []中的圖片數據,我可以簡單地使用json返回所有數據,而不需要另一個http請求只爲圖片。
我的PHP技能不是很好,但我想大概是這樣的:
$posts['picture'] = base64_encode($posts['picture']);
但我需要每一個畫面爲Base64轉換,所以也許它能夠更好地把它放在while循環:
while($post = mysql_fetch_assoc($result)) {
if($post == 'picture'){
$post = base64_encode($post);
}
$posts[] = array('post'=>$post);
}
可以工作嗎?或者還有其他更好的方法嗎? 謝謝!