Select random

Status
Not open for further replies.

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
Hello,

I'm trying to get MySQL to select 4 random people from a database each time the page is refreshed but I'm not sure how I can accomplish this.

Can someone please help?

Thanks.
 

Ecko

23:37 [autobots] -!- eckostylez [[email protected]]
Nov 25, 2012
1,399
962
Show table structure or the query you currently have first, then we can know the best way to do this. I would say use ORDER BY RAND() in the SELECT statement, but that is inefficient as you waste resources creating a temporary table.
 

NSA

sudo apt-get thefuckout.tar.gz
Dec 9, 2011
715
86
I'm using the Phoenix database and I want to select 4 random users from the users table.
 

Kaz

BooYah
Staff member
Nov 16, 2010
3,064
1,025
Funnily enough i have just implemented this onto my site, you will need to change it up abit as i have just copied the code from my script

PHP:
$getRandUser = $db->query("SELECT * FROM users ORDER BY RAND() LIMIT 4");
while($getUser = $db->fetch_array($getRandUser)) {
echo $getUser['username'];
}

Then obviously where you echo it, you can have your div or w/e you want to display
 
Status
Not open for further replies.

Users who are viewing this thread

Top