rrirower
rrirower

Reputation: 4590

Returning SQL Blob from PHP JSON

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.

Upvotes: 3

Views: 4633

Answers (1)

konsolenfreddy
konsolenfreddy

Reputation: 9671

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);

Upvotes: 1

Related Questions