我正在尝试创建一个社交网络。在个人资料详细信息中,我想显示该个人资料用户的至少10个朋友。我用过:
$connect = mysqli_connect("...","...","...","dbname"); //NOT REALLY IMPORTANT
$find = "SELECT * FROM friendslist WHERE user1 = '$myusername'";
$exec = mysqli_query($connect,$find);
while($row=mysqli_fetch_assoc($exec)) {
$friendusername = $row['user2'];
$con2 = "SELECT * FROM users WHERE username = '$friendusername' ";
$exec2 = mysqli_query($con,$con2);
$row2 = myslqi_fetch_assoc($exec2);
echo $row2['firstname']."<br/>";
我的桌子:
FOR friends:
id, user1, user2
FOR users
id, username, firstname
它会显示我的所有朋友,如果我有20个朋友,它将显示所有20个朋友。如何将其限制为仅显示10?
答案 0 :(得分:0)
尝试:
$find = "SELECT * FROM friendslist WHERE user1 = '$myusername' LIMIT 0, 10";
答案 1 :(得分:0)
就像@ Danijel的答案一样,很好的选择是加入表格。
首先加入表格,使用 f.user1 ='$ myusername'获取好友列表,然后将其限制为十个。
PD:不要在循环中进行查询,这对性能不利
$find = "SELECT *
FROM friendslist f, users u
WHERE f.user2 = u.username
AND f.user1 = '$myusername'
LIMIT 10";
$exec = mysqli_query($connect,$find);
while($row=mysqli_fetch_assoc($exec)) {
echo $row2['firstname']."<br/>";
}
答案 2 :(得分:0)
加入查询并限制为10。
"SELECT * FROM friendslist LEFT JOIN users ON friendslist.user2=users.username WHERE user1 = '$myusername' LIMIT 10"