1

I have tried this but not working.

 $getPerson = mysqli_query($con,"SELECT * FROM Persons LIMIT 0,15");
 shuffle($getPerson)//how to set limit for this?
Muhammad Shahzad
  • 9,340
  • 21
  • 86
  • 130
Ivan
  • 93
  • 1
  • 8
  • i want to display only 4 from the database using shuffle. how is that possible? pls help. thank you. – Ivan Jun 08 '16 at 04:07

1 Answers1

1

You can use ORDER BY RAND() with LIMIT 0,4

SELECT * FROM Persons ORDER BY rand() LIMIT 0,4
kamal pal
  • 4,187
  • 5
  • 25
  • 40
  • rand() will make it slow. are there any other alternatives besides rand()? – Ivan Jun 08 '16 at 04:24
  • @Ivan no, I don't think so .. check it out http://stackoverflow.com/questions/1823306/mysql-alternatives-to-order-by-rand – kamal pal Jun 08 '16 at 04:26
  • Check out the accepted answer on this page: http://stackoverflow.com/questions/6541644/selecting-random-rows-with-mysql . It looks like using the PHP `rand()` function might be faster than the mysqli `RAND()` function, at least for large databases. – Sgt AJ Jun 08 '16 at 05:05
  • how can it be... if you have to return a large number of records and then shuffle them, surely faster to return a small random set only – Barry Jun 08 '16 at 05:14
  • Using PDO might help but I'd still guess that PHP's rand() would be quicker – CrabLab Jun 08 '16 at 06:40