本文介绍了PHP MySQL选择随机行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在选择6个随机朋友时遇到问题
I have a problem selecting 6 random friends
这是我到目前为止得到的查询:
This is the query I've got so far:
$result = num_rows("SELECT * FROM friends WHERE member_id = '".$_SESSION['userid']."'");
if($result >= 6) {
$f_num = 6;
} else {
$f_num = $result;
}
for($i = 1; $i <= $f_num; $i++) {
$q_get_member_friends = mysql_query("SELECT * FROM friends WHERE member_id = '".$_SESSION['userid']."' ORDER BY rand() LIMIT 1");
$r_get_member_friends = mysql_fetch_array($q_get_member_friends);
echo $r_get_member_friends['friend_with'];
}
如果登录的用户有6个以上朋友,我想随机选择6个朋友
I want to select 6 random friends if the logged in user has more or equal to 6 friends
现在坚持了一段时间:/
Stuck on this for a while now :/
感谢您的帮助:)
推荐答案
如果使用:
SELECT *
FROM friends
WHERE member_id = '".$_SESSION['userid']."'
ORDER BY rand()
LIMIT 6
如果该人只有3个朋友,查询将仅显示这三个朋友-这并不意味着查询将始终返回六行.
If the person only has 3 friends, the query will only show those three - it doesn't mean that the query will always return six rows.
这篇关于PHP MySQL选择随机行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!