Get random record in table


/ Published in: MySQL
Save to your folder(s)

This code is useful if you have a table where some records have been deleted. This code will favor records after large blocks of deleted records.

1) Get max id
2) generate random number with the max id
3) Query DB with random number.


Copy this code and paste it in your HTML
  1.  
  2. <?php
  3. $random_num = rand(0, $max(id));
  4. ?>
  5.  
  6. SELECT * FROM table WHERE id >= $random_num LIMIT 1

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.