【发布时间】:2020-11-25 01:13:10
【问题描述】:
我有 3 个表格
notifications
not_id | not_name
-------------------------
2 | Notification Name 01
3 | Notification Name 02
4 | Notification Name 03
groups
group_id | group_name
-------------------------
4 | group name 1
5 | group name 2
group_not
---------------------------
group_not_id | group_id | not_id
---------------------------
1 | 4 | 2
2 | 4 | 3
3 | 5 | 4
我想显示所有与 group_id = 4 的组相关的通知
但php端显示重复如下:
Notification Name
Notification Name 01
Notification Name 01
Notification Name 02
Notification Name 02
MYSQL 代码
function getRows_not_group($group_id)
{
global $conn;
$sql = "SELECT group_not.group_not_id, notifications.not_name, groups.group_name FROM group_not JOIN groups ON group_not.group_id = $group_id JOIN notifications ON group_not.not_id = notifications.not_id WHERE group_not.group_id = $group_id";
$result = mysqli_query($conn, $sql);
if(!$result)
{
echo mysqli_error($conn);
}
$rows = [];
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_assoc($result))
{
$rows[] = $row;
}
}
return $rows;
}
【问题讨论】:
-
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!