PHP/MySQLを使用してクエリを実行し、JSONとしてエンコードしますが、必要な形式にJSONを取得する方法がわかりません。ここでMySQLからJSON - 2つのクエリ/書式の組み合わせ
は私のPHPです:
$myquery1 = "select 'links' as type, source, target, value from table";
$myquery2 = "select 'nodes' as type, name from table2";
$query = mysql_query($myquery1);
if (! $query) {
echo mysql_error();
die;
}
$data = array();
for ($x = 0; $x < mysql_num_rows($query); $x++) {
$data[] = mysql_fetch_assoc($query);
}
//(and again for myquery2)
echo json_encode($data); //not sure how to combine queries here
私はこのような "タイプ" によってグループ化グループ化するJSONをしたいと思います:
{
"links": [{"source":"58","target":"john","value":"95"},
{"source":"60","target":"mark","value":"80"}],
"nodes":
[{"name":"john"}, {"name":"mark"}, {"name":"rose"}]
}
すべてのヘルプははるかに高く評価されます。ありがとうございました!
***してください[ 'mysql_ *'関数を使用して停止](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions- PHP 7で削除されました。[準備中](http://php.net/manual/ja/migration70.removed-exts-sapis.php) //en.wikipedia.org/wiki/Prepared_statement)[PDO](http://php.net/manual/en/pdo.prepared-statements.php)と[MySQLi](http://php.net /manual/en/mysqli.quickstart.prepared-statements.php)、PDOの使用を検討してください。[これは本当に簡単です](http://jayblanchard.net/demystifying_php_pdo.html)。 –
あなたは ''links''と'' types''の前後で引用符を使用しています。これは構文エラーを投げます。 –
@JayBlanchardそれは文法エラーを投げません。 backquick:singlequoteの代わりに '\' 'を使用してください。 – Halcyon