PHP MySQL select random rows

前端 未结 6 1468
深忆病人
深忆病人 2021-02-15 16:47

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 = \'\".$_S         


        
6条回答
  •  有刺的猬
    2021-02-15 17:40

    First select the number of friends that the user has:

    "SELECT COUNT(*) as numFriends FROM friends WHERE member_id='".$_SESSION['userid']."'
    

    ...put that into a variable, let's call it "$numFriends" Then:

    for($z=0;$z<6;$z++)
    {
       $randomFriendIndex = rand(1,$numFriends);
       //Get the friend at that index
    }
    

提交回复
热议问题