dqch34769 2016-09-12 08:50
浏览 102

我想将数据库中的整个blob(.bin)文件保存到数组中

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?

  • 写回答

1条回答 默认 最新

  • dongzg2006 2016-09-12 09:09
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False