带有BLOB变量的PHP Slim GET请求

带有BLOB变量的PHP Slim GET请求,php,mysql,blob,slim,Php,Mysql,Blob,Slim,我正试图在PHP Slim框架中执行一个fetch请求。但是在我的MYSQL数据库中有一个BLOB变量。它包含字符串的数组。如何检索BLOB变量并将其转换回字符串的数组 这是我的取回方法 // fetch all apps $app->get('/apps', function ($request, $response, $args) { $sth = $this->db->prepare("SELECT * FROM app ORDER BY updated_at")

我正试图在
PHP Slim框架
中执行一个fetch请求。但是在我的
MYSQL
数据库中有一个
BLOB
变量。它包含
字符串的
数组
。如何检索
BLOB
变量并将其转换回
字符串的
数组

这是我的取回方法

// fetch all apps
$app->get('/apps', function ($request, $response, $args) {
    $sth = $this->db->prepare("SELECT * FROM app ORDER BY updated_at");
    $sth->execute();
    $apps = $sth->fetchAll();
    return $this->response->withJson($apps);
});

这里的
$apps
有一个名为
locale
的变量,它是
BLOB
。我想用
字符串的
数组
返回
$apps
,我做错了。我试图将
Array
对象直接保存到
BLOB
中。因此,在检索它时,我得到的是一个字符串“Array”,而不是我的
Array
数据

现在,我将
数组
编码为
JSON字符串
,然后将其保存到
BLOB
变量中

在检索时,我将
字符串
解码回
JSON

这就是我保存应用程序的方式

// add new app
$app->post('/saveApp', function ($request, $response) {
    $input = $request->getParsedBody();
    $input['locale'] = json_encode($input['locale']);

    $sql = "INSERT INTO app (id,name,locale) VALUES (:id,:name,:locale)";
    $sth = $this->db->prepare($sql);
    $sth->bindParam("id", $input['id']);
    $sth->bindParam("name", $input['name']);
    $sth->bindParam("locale", $input['locale']);
    $sth->execute();
    $input['id'] = $this->db->lastInsertId();
    return $this->response->withJson($input);
});
这就是我获取应用程序的方式

// fetch all apps
$app->get('/apps', function ($request, $response, $args) {
    $sth = $this->db->prepare("SELECT * FROM app ORDER BY updated_at");
    $sth->execute();
    $apps = $sth->fetchAll();
        $result = array();
        foreach ($apps as $app) {
            $app['locale'] = json_decode($app['locale']);
            array_push($result, $app);
        }
    return $this->response->withJson($result);
});

我做错了。我试图将
Array
对象直接保存到
BLOB
中。因此,在检索它时,我得到的是一个字符串“Array”,而不是我的
Array
数据

现在,我将
数组
编码为
JSON字符串
,然后将其保存到
BLOB
变量中

在检索时,我将
字符串
解码回
JSON

这就是我保存应用程序的方式

// add new app
$app->post('/saveApp', function ($request, $response) {
    $input = $request->getParsedBody();
    $input['locale'] = json_encode($input['locale']);

    $sql = "INSERT INTO app (id,name,locale) VALUES (:id,:name,:locale)";
    $sth = $this->db->prepare($sql);
    $sth->bindParam("id", $input['id']);
    $sth->bindParam("name", $input['name']);
    $sth->bindParam("locale", $input['locale']);
    $sth->execute();
    $input['id'] = $this->db->lastInsertId();
    return $this->response->withJson($input);
});
这就是我获取应用程序的方式

// fetch all apps
$app->get('/apps', function ($request, $response, $args) {
    $sth = $this->db->prepare("SELECT * FROM app ORDER BY updated_at");
    $sth->execute();
    $apps = $sth->fetchAll();
        $result = array();
        foreach ($apps as $app) {
            $app['locale'] = json_decode($app['locale']);
            array_push($result, $app);
        }
    return $this->response->withJson($result);
});

字符串数组如何存储到blob中?当前代码的JSON输出是什么?字符串数组如何存储到blob中?当前代码的JSON输出是什么?