• 0

[PHP] Select random from database


Question

Hopefully I can explain this quite simply.

In the database I have a number of places like:

ID NAME

1 John

2 Mary

5 Nigel

in its simplest form, the entries get added and removed regularly so therefore there will be gaps in the unique ID.

I need to be able to select a random ID number.

I cannot simply count them and pick a random number because it may not exist.

I am sure there is a very simple way to do this, I guess I have eaten too much over Christmas to work it out.

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

SELECT id, name FROM table ORDER BY RAND() LIMIT 1;

This can get fairly expensive on large data-sets though.

;)

Brilliant, only ever 100 max so be perfect.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.