PHP + MySQL - Random list all the data of the MySQL table

advertisements

I got a table named clients in MySql. I want to list all the data from that table randomly in a php webpage. Is it possible to random the data whenever user visit the php webpage?

Thank you :D


You could do it like this:

SELECT * FROM mytable ORDER BY RAND();

This is mentioned in an example here. As mentioned in the documentation, rand() doesn't generate perfectly random numbers/sequence (but it will probably suffice for practical use on a website).

You could also use limit to select only N (e.g. 5) records:

SELECT * FROM mytable ORDER BY RAND() LIMIT 5;