0

I have a database with a data (.bin/ blob file) stored into it. I'm using the following code to output the data from the .bin file:

function connect_db() {
    $server = '****';
    $user = '****';
    $pw = '****';
    $db = '****';
    $connect = mysqli_connect($server,$user,$pw,$db);
    if(!$connect) {
        die('Connection error: '.mysqli_connect_error());
    }
    return $connect;
}

function get_blob() {
    $connect = connect_db();
    $query = 'SELECT blob FROM my_table';
    $result = mysqli_query($connect, $query);
    while($data = mysqli_fetch_assoc($result)) {
         header('Content-type: text/plain');
         $blob[]=$data;
    }
    return $blob;
}

echo '<pre>';
print_r(get_blob());
echo '</pre>';

This gives me the following output (this is one of the many values in the array that I get back from the database):

[24] => Array
    (
        [data] => a:1:{s:7:"context";a:5:{s:11:"product_ids";s:6:"entity";s:19:"add_to_cart_combine";i:1;s:30:"show_single_product_attributes";i:1;s:12:"display_path";s:8:"node/171";s:6:"entity";a:3:{s:11:"entity_type";s:4:"node";s:9:"entity_id";s:3:"171";s:28:"product_reference_field_name";s:14:"field_products";}}}

I was looking for an image, but it seems this blob doesn't contain one. However I still would like to save all this data correctly into an array (multidimensional probably). I was planning to use explode, but that seems a little far fetched. Is there a way for me to get clean data without using explode too much?

hlh3406
  • 1,382
  • 5
  • 29
  • 46
B. Jansen
  • 26
  • 9
  • From the looks of it, the content of the blob seems to be an array already, have you tried saving it as such? – Nagarz Sep 12 '16 at 08:53
  • This much more seems like serialzed thingy. Tried `unserialize()`? – Royal Bg Sep 12 '16 at 08:53
  • I tried unserialize. That just gives me a huge empty array back (3000+ keys). I'll see if I can save it as an array as is, but eventually each different value in the blob needs to be a different item, since I have to parse it into an xml file. – B. Jansen Sep 12 '16 at 08:57
  • I guess I did something wrong while using unserialize, since an online unserializer works perfect. I'll look into it some more, thanks for the answer – B. Jansen Sep 12 '16 at 08:59
  • http://stackoverflow.com/a/38829952/267540 – e4c5 Sep 12 '16 at 10:47
  • @e4c5 I already fixed it with unserialize(), but thanks for the link anyway. I will look into it. – B. Jansen Sep 12 '16 at 10:53

1 Answers1

0

The data is JSON encoded

Replace

$blob[]= $data;

with

$blob[]= json_decode($data);

Or maybe (it's not exactly clear from your post)

$blob[]= json_decode($data['data']);

or

$blob[]= json_decode($data['blob']);

See http://php.net/manual/en/function.json-decode.php

svn
  • 1,235
  • 10
  • 22
  • I used unserialize, and I get a clean print_r back now. The problem now is that this .bin file does not contain an image, so I have to look through thousands of bin files to hopefully find the correct path to the server. Thanks for the answer anyway! – B. Jansen Sep 12 '16 at 09:45