#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    England, UK
    Posts
    83
    Rep Power
    15
    Hi,
    I'm using PHP3, does anyone know how I can pull a random row from a mySQL table?

    Girish Gupta

    PS - Can you please give me code, not just tell me what to do as I am a beginner!
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    24
    Rep Power
    0
    Just check the mysql version .... the latest version has this feature ....
    use can use :
    select * from tablename order by rand()


    But if the above doesn't work then the version of mysql doesn't have the random feature .... there is another way around it

    firstly u have to have an auto increment column (won't work if u dont) .. say its called ID. Then the sql statement u would use would be:

    select *, ID*0+rand() as rand_col from tablename order by rand_col

  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    England, UK
    Posts
    83
    Rep Power
    15
    I've just worked it out:

    1) Find out how many results = x
    2) Get random number between 1 and x
    3) Select from db where id = x

    Thanks.
  6. #4
  7. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Location
    England, UK
    Posts
    83
    Rep Power
    15
    No, looks like your way is better!!!

Similar Threads

  1. Selecting a random row
    By TomGillespie in forum MySQL Help
    Replies: 3
    Last Post: February 16th, 2004, 08:34 AM
  2. Replies: 1
    Last Post: January 30th, 2004, 07:04 PM
  3. Unable to delete row from mysql table. Please help!
    By snkhan in forum PHP Development
    Replies: 1
    Last Post: January 24th, 2004, 10:47 PM
  4. how to keep table on top, not centered??
    By vinyl in forum HTML Programming
    Replies: 2
    Last Post: January 20th, 2004, 07:19 PM
  5. Validate PHP form and update MySQL table
    By tlugosi in forum PHP Development
    Replies: 2
    Last Post: January 18th, 2004, 03:26 PM

IMN logo majestic logo threadwatch logo seochat tools logo