PHP 带有 BLOB 变量的 Slim GET 请求
PHP Slim GET request with BLOB variable
我正在尝试在 PHP Slim framework
中执行提取请求。但是我的 MYSQL
数据库中有一个 BLOB
变量。其中包含 array
个 strings
。如何检索 BLOB
变量并将其转换回 strings
的 array
。
这是我的获取方法
// 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
。我想 return $apps
和 array
共 strings
我做错了。我试图将 Array
对象直接保存到 BLOB
中。由于检索它,我得到了一个字符串 "Array" 而不是我的 Array
数据。
现在,我将 array
编码为 JSON String
,然后将其保存到 BLOB
变量中。
在检索它时,我正在将 string
解码回 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);
});
我正在尝试在 PHP Slim framework
中执行提取请求。但是我的 MYSQL
数据库中有一个 BLOB
变量。其中包含 array
个 strings
。如何检索 BLOB
变量并将其转换回 strings
的 array
。
这是我的获取方法
// 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
。我想 return $apps
和 array
共 strings
我做错了。我试图将 Array
对象直接保存到 BLOB
中。由于检索它,我得到了一个字符串 "Array" 而不是我的 Array
数据。
现在,我将 array
编码为 JSON String
,然后将其保存到 BLOB
变量中。
在检索它时,我正在将 string
解码回 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);
});