0

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

0

3 Answers 3

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I tried to use it in my code. It's working but after I click to next page, some of the data in page 1 appear in page 2 and so on. I tried to edit my php code but it's not working. Do I need to use php coding instead of ORDER BY Rand()? Thanks :D
@user1822825 Yes, each time the query is run (it will be run each time you load the PHP-script, which will happen on "next page" etc), things will be randomized all over. If you want to avoid this, I think you're better off doing the randomization in PHP rather than the query. Some logic will be needed to avoid re-randomization between pages. I think that will be a question for another post, where you clarify and specify your needs...
2

Add an ORDER BY RAND() clause to your query.

One warning though, this sorting will not be very efficient...

Comments

1
SELECT * FROM clients
ORDER BY RAND()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.