Welcome Guest, Not a member yet? Register   Sign In
Selecting a random result that changes each time the page refreshes
#1

[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.
#2

[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.
#3

[eluser]maldoinc[/eluser]
You should use the following query to get the desired results

Code:
select * from testimonials order by rand() limit 1
#4

[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
#5

[eluser]maldoinc[/eluser]
Considering the table is for testimonials it should be fine, unless his testimonials table has 1k+ records.
#6

[eluser]CroNiX[/eluser]
Plan for growth, not staying small Smile
#7

[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!




Theme © iAndrew 2016 - Forum software by © MyBB