Home » Php » php – Selecting Random Result from MySQL

php – Selecting Random Result from MySQL

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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

Answer:

You need ORDER BY RAND()

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

Answer:

MySQL uses RAND() instead of RANDOM().

Answer:

Since it’s MySQL, you need to use RAND() instead of RANDOM().

Answer:

This is a very good source of advice for efficiently retrieving random records from a table with MySQL.

http://www.dasprids.de/blog/2008/06/07/fetching-random-rows-of-mysql-efficiently

You might want to check it out.