データベースに接続し、テーブルからデータを取得してjson形式で表示するphpファイルを作成しました。
index.php という名前のファイル。
json を表示するには、ブラウザでファイルに移動します。
http://127.0.0.1/json/index.php and it displays:
{"title":[{"id":"1","title":"Title1","desc":"Description1"},{"id":"2","title":"Title2","desc":"Description2"}]}
私がする必要があるのは、次のようなパラメータを追加してこれをフィルタリングできるようにすることです:
For example: http://127.0.0.1/json/index.php?id=1 to just show the data with an id of 1 but it still shows all the data.
phpコードは次のとおりです。
<?php
$username = "root";
$password = "";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
$selected = mysql_select_db("mydb",$dbhandle)
or die("Could not select mydb");
$result = mysql_query("SELECT * FROM contacts");
$rows = array();
while($r = mysql_fetch_assoc($result)) {
$rows['title'][] = $r;
}
print json_encode($rows);
?>
ここで間違っていること、または行方不明になっていることは何ですか?