1
私は初心者の開発者であり、それを理解することは非常に困難です。 私を助けてください。2つのテーブルからjson配列に行を選択
catbook
+----+---------+------------+
| id | cat_num | count_book |
+----+---------+------------+
| 1 | 55555 | 21 |
| 2 | 77777 | 40 |
+----+---------+------------+
userbook
+----+-----------+--------------+------------+
| id | user_name | cat_userbook | read_count |
+----+-----------+--------------+------------+
| 1 | Andy | 55555 | 3 |
| 2 | Andy | 77777 | 5 |
| 3 | Tom | 55555 | 4 |
| 4 | Tom | 77777 | 8 |
+----+-----------+--------------+------------+
コード
<?php
require_once("config.php");
$user_name = "Andy";
$sql ="SELECT cat_userbook, read_count, count_book FROM userbook, catbook WHERE user_name LIKE '" . $user_name . "'";
$result = mysqli_query($db_connect, $sql);
$response = array();
while ($row = mysqli_fetch_assoc($result)) {
$response[] = $row;
}
echo json_encode($response);
mysqli_close($db_connect);
結果:
[
{"cat_userbook":"55555","read_count":"3","count_book":"21"},
{"cat_userbook":"55555","read_count":"3","count_book":"40"},
{"cat_userbook":"77777","read_count":"5","count_book":"21"},
{"cat_userbook":"77777","read_count":"5","count_book":"40"}
]
が、それはので、私のために必要であるとして、私はこの結果をしたい:
[
{"cat_userbook":"55555","read_count":"3","count_book":"21"},
{"cat_userbook":"77777","read_count":"5","count_book":"40"}
]
返すようにDBサーバーに伝える必要がありますここで、 'cat_num = cat_userbook'となります。 [JOINS](https://www.w3schools.com/sql/sql_join.asp)について学んでください –