私のWebサーバーでは、PHPスクリプトでget_result()
を使用することはできません。エラーが発生しました未定義のメソッドmysqli_stmt :: get_resultをに呼び出します。json_encode without get_result()
しかし、bind_result()
とfetch()
が動作しますが、SELECT結果に応じて毎回異なる列を宣言する必要がありますが、一般的にしたいと思います。
Iが得た最も近い次のコードであった:
function bind_result_array($stmt)
{
$meta = $stmt->result_metadata();
$result = array();
while ($field = $meta->fetch_field())
{
$result[$field->name] = NULL;
$params[] = &$result[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
return $result;
}
function getCopy($row)
{
return array_map(create_function('$a', 'return $a;'), $row);
}
$stmt = $mysqli->prepare("SELECT 1 as userId, 2 as message UNION SELECT 3 as userId, 4 as message");
$stmt->execute();
$row = bind_result_array($stmt);
if(!$stmt->error)
{
while($stmt->fetch())
$genericArray[$row['userId']] = getCopy($row);
}
echo "{ \"userId\": ";
echo json_encode($genericArray);
echo "}";
$stmt->close();
$mysqli->close();
本の出力である:
{ "はuserId":{ "1":{ "はuserId" :1、 "メッセージ":2 }、 "3":{ "はuserId":3、 "メッセージ":4 } } }
それがすべきである:
{ "はuserId":[{ "はuserId":1、 "メッセージ":2 }、 { 「はuserId 「:1、 "メッセージ":2 }]}
ホーw jsonレスポンスのこの追加レベルを取得し、それをjson形式の配列にしますか? または、コードを毎回変更することなく、json形式でSELECT結果を得る他の方法があります。
の問題は、$ genericArray [$ row ['country_id']] = getCopy($ row);という行にあります。結果にcountry_idの値はありません。したがって、あなたのコメントのために " –
ありがとうございました。しかし、それはちょうど私の質問が正しく尋ねられなかったことを私に気づかせました。実際、私のターゲットはjson([...])でも配列されています。私はまだ、いくつかの助けが必要だと思います。 – Graffl