3

I've read many posts regarding PHP/JSON blob processing, but, none seem to provide a clear cut example on how to proceed. I am trying to communicate with an SQL database on a server. The table in question has a medium blob field. I use PHP and JSON to grab the data and display it on an Android phone. I have no problem with the non-blob data. I can grab it and display it with no problems. However, the blob data always returns as "null" via JSON. I understand that JSON does not directly support binary data, but, my question still remains, how do I access the blob data from the Android system? The blob data in question is mostly text files (ie. .PDF, .DOC, etc.). I have no control over the SQL table. More importantly, how do I get the blob field packaged with the rest of the data so I can process it on the Android platform? Any help would be greatly appreciated.

rrirower
  • 4,338
  • 4
  • 27
  • 45
  • possible duplicate http://stackoverflow.com/questions/4855447/how-to-use-blob-with-json-and-php – Sergey Benner Feb 06 '12 at 18:29
  • Hey - For some scenarios base64 encoding of binary data solves many problems. It will have a size of 3/2 the original data on the wire which is a problem, but all data is alphanums, why it is easy to transfer. It causes overhead both server and client side though and I dunno about php support, but perhaps? – faester Feb 06 '12 at 18:31

1 Answers1

1

Storing the binary data in a database table might be a bad idea, see this question: Storing Images in DB - Yea or Nay?

You can use base64_encode() in PHP to encode the binary data.

In PHP it might look something like this:

$row = mysql_fetch_assoc($res);
foreach($row as $key => $value){
    $json[$key] = base64_encode($value);
}
echo json_encode($json);
Community
  • 1
  • 1
konsolenfreddy
  • 9,551
  • 1
  • 25
  • 36