json - encode/replace blob with base64 in php array -
iam building html5 app gets data json encoded php webservice. have small images stored in database , return them base64 encoded json result.
currently mysql field , return json this:
$query = "select * `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));
i have field called picture. convert picture data in array posts[] base64 can return data json without http request picture.
my php skills not thought like:
$posts['picture'] = base64_encode($posts['picture']);
but need convert every picture base64, maybe better put in while loop:
while($post = mysql_fetch_assoc($result)) { if($post == 'picture'){ $post = base64_encode($post); } $posts[] = array('post'=>$post); }
can work? or there other/better method? thanks!
not really, first, mysql_fetch_assoc return entire row (all columns), means $post
contain each column name, like:
$post['column_name_a'] $post['column_name_b'] $post['column_name_c'] ....
so, should (it doesn't matter if empty):
$post['picture'] = base64_encode($post['picture']);
then, add $posts[] array , json_encode() print.
as side note, mysql_fetch_assoc deprecated php 5.5 , removed in future -- see http://php.net/manual/en/function.mysql-fetch-assoc.php
Comments
Post a Comment