2017-04-04 4 views
1

PHP Slim frameworkでフェッチリクエストを実行しようとしています。しかしMYSQLデータベースにBLOBという変数があります。これはarraystringsを含んでいます。変数BLOBを取得し、arraystringsに戻すにはどうすればよいですか。BLOB変数付きPHPスリムGETリクエスト

はここ$appsBLOBあるlocaleという名前の変数を持ってここに私の取得方法

// 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); 
}); 

です。返信したい$appsarraystrings

+0

を取得していますどのようにこれはアプリ

// 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); }); 

を保存していますどのようにでしょうか?現在のコードでのJSON出力は何ですか? –

答えて

0

私は間違っていました。私はに直接オブジェクトArrayを保存しようとしていました。そのため、私はArrayデータの代わりに文字列 "Array"を取得していました。

今、arrayJSON Stringにエンコードし、それをBLOB変数に保存しています。

これを検索すると、私はstringJSONにデコードしています。

は、これは私がアプリを文字列の配列を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(); 
     $result = array(); 
     foreach ($apps as $app) { 
      $app['locale'] = json_decode($app['locale']); 
      array_push($result, $app); 
     } 
    return $this->response->withJson($result); 
});