我有这个跟随系统,它使用两列,第一列id跟在第二列id之后,问题是我正在尝试做一个关注者页面,它显示谁在关注那个人,我需要得到用户表中与该ID相关的用户名以及我无法执行的用户名
这不会回应任何事情
<?php include_once("php_includes/db_conx.php"); ?>
<html>
<head>
<title>Followers</title>
</head>
<body>
<?php
$profileid=$_GET['profid'];
$query = mysqli_query($db_conx,"SELECT user_one FROM Follow WHERE user_two='$profileid'");
$queryname = mysqli_query($db_conx, "SELECT username FROM users WHERE id='$id'");
While($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
$id = $row["user_one"];
while($name= mysqli_fetch_array($queryname, MYSQLI_ASSOC)){
$name1 = $name["username"];
echo $name1;
}
}
?>
</body>
</html>
我可以得到它来回应那些关注者的ids,但是当我按照那个ID获取用户名的时候,它却没有给我任何东西
答案 0 :(得分:0)
使用联接而不是进行两次查询
类似:
SELECT user_one FROM Follow
LEFT JOIN users ON user.id = follow.user_id
WHERE follow.id='$profileid'
AND $profileid
不应该在sql中。它应该是:
WHERE follow.id=?
答案 1 :(得分:0)
这有助于您了解联接:
<?php include_once("php_includes/db_conx.php"); ?>
<html>
<head>
<title>Followers</title>
</head>
<body>
<?php
$profileid=$_GET['profid'];
$query = mysqli_query($db_conx,"SELECT u.username as username FROM Follow as f JOIN users as u on u.id = f.user_one WHERE f.user_two=".(int)$profileid);
While($row = mysqli_fetch_assoc($query)) {
echo $row['username'];
}
?>
</body>
</html>