1

I have a MySQL database with pictures within it. When a user comes to my page I want to show him random picture and from then he can go to the next or previous picture (not random)

Database example

id | picture 
11 | foto1.gif
12 | foto2.gif
16 | foto3.gif
23 | foto4.gif
66 | foto5.gif

How can I make something like that with PHP and SQL? Thank you!

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
Nathaniel
  • 43
  • 1
  • 3
  • possible duplicate of [What is the best way to pick a random row from a table in MySQL?](http://stackoverflow.com/questions/142242/what-is-the-best-way-to-pick-a-random-row-from-a-table-in-mysql) – Bobby Apr 18 '11 at 13:57
  • It's not a duplicate. The linked question does not mention retrieving the next/previous entry. – ThiefMaster Apr 18 '11 at 13:58

1 Answers1

2

Use ORDER BY rand() in your query to get the random item.

To retrieve the next/previous item (by id), use this:

WHERE id < current_id ORDER BY id DESC LIMIT 1   (for prev)
WHERE id > current_id ORDER BY id ASC LIMIT 1   (for next)
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636