Selecting a random result that changes each time the page refreshes - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Selecting a random result that changes each time the page refreshes (/showthread.php?tid=48913) |
Selecting a random result that changes each time the page refreshes - El Forum - 02-01-2012 [eluser]mikegeorgeff[/eluser] I've been trying to figure this one out for a while now. I have a table filled with client testimonials, and I want to place one random result into the footer that will change each time the page is refreshed. Selecting a random result that changes each time the page refreshes - El Forum - 02-01-2012 [eluser]CroNiX[/eluser] There is no efficient way to do this in mysql AFAIK. From what I've read, the best way is to select MAX(id) in one query, then use php to get a random number between 1 and that number and then do a 2nd query to get the record. However, that assumes no rows have been deleted. Selecting a random result that changes each time the page refreshes - El Forum - 02-02-2012 [eluser]maldoinc[/eluser] You should use the following query to get the desired results Code: select * from testimonials order by rand() limit 1 Selecting a random result that changes each time the page refreshes - El Forum - 02-02-2012 [eluser]CroNiX[/eluser] Try doing that on a large table. It can take forever to get a result. http://www.webtrenches.com/post.cfm/avoid-rand-in-mysql Selecting a random result that changes each time the page refreshes - El Forum - 02-02-2012 [eluser]maldoinc[/eluser] Considering the table is for testimonials it should be fine, unless his testimonials table has 1k+ records. Selecting a random result that changes each time the page refreshes - El Forum - 02-02-2012 [eluser]CroNiX[/eluser] Plan for growth, not staying small Selecting a random result that changes each time the page refreshes - El Forum - 02-02-2012 [eluser]InsiteFX[/eluser] Every time the page is refreshed it will access the database now think what will happen if you have a 1000+ users online! |