带有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输出是什么?