Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting Random Result from MySQL

Tags:

php

mysql

I wanted to randomly select results from mysql database with this code:

$data = mysql_query("SELECT * FROM people ORDER BY RANDOM() LIMIT 4") or die(mysql_error()); 

I got an error message: FUNCTION members.RANDOM does not exist

Is there something I'm not adding or doing right here?

Thanks for your asistance.

like image 831
Alan Avatar asked Aug 08 '11 15:08

Alan


2 Answers

The function name you're looking for is RAND().

like image 109
JK. Avatar answered Oct 04 '22 02:10

JK.


You need ORDER BY RAND()

$data = mysql_query("SELECT * FROM people ORDER BY RAND() LIMIT 4") or die(mysql_error());
like image 41
Michael Berkowski Avatar answered Oct 04 '22 02:10

Michael Berkowski