Results 1 to 9 of 9

Thread: Randomly returning rows

  1. #1
    Registered Mike's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    2,755

    Randomly returning rows

    Hi all,

    I would like to randomly return rows from a basic database. I would do this through rand() but it would take quite a while with all the if statements imho.

    So can anyone think of a quicker way?

    Thanks,
    Mike
    Don't you just love free internet games ?

  2. #2
    Senior Member chromate's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    2,348
    if statements?

    Just count the number of rows returned and set the range of the rand function from 0 to count_num_rows($result). Then use the number returned to pull that row from the results.

    Is this what you mean?

  3. #3
    Registered Mike's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    2,755
    Sorry, I should have explained better. I want about ten results to be returned, that's why I said if statements.

    Or would you just do what you said ten times?
    Don't you just love free internet games ?

  4. #4
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    If you knew the total number of rows you can generate 10 random numbers and then query the DB with those in a single query (where rowid in (55, 56, 87,...)
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

  5. #5
    Registered Mike's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    2,755
    Hmm... do you know of a tutorial Chris please?

    Thanks,
    Mike
    Don't you just love free internet games ?

  6. #6
    Administrator Chris's Avatar
    Join Date
    Feb 2003
    Location
    East Lansing, MI USA
    Posts
    7,055
    An SQL tutorial covering where statements should show an example.
    Chris Beasley - My Guide to Building a Successful Website[size=1]
    Content Sites: ABCDFGHIJKLMNOP|Forums: ABCD EF|Ecommerce: Swords Knives

  7. #7
    Senior Member chromate's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    2,348
    PHP Code:
    for ($i=0$i 10$i++) {
       
    $rnd_row rand(0$total_rows);
       if (
    $i 0$row_ids "rowid = '$rnd_row' ";
       
    $row_ids .= "OR rowid = '$rnd_row' ";
    }

    $sql "SELECT * FROM blah WHERE $row_ids";
    $result mysql_query($sql$db); 
    Something like that... Probably wont work right first time... I just bashed it out quickly cause I don't have much time

  8. #8
    Registered Mike's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    2,755
    I'll try it tomorrow, thanks Chromate
    Don't you just love free internet games ?

  9. #9
    Half of it is 70% smarts Nick's Avatar
    Join Date
    Jan 2004
    Location
    Denver
    Posts
    104
    You could also put all the rows into an array, and use a random number to pull from the array.

Similar Threads

  1. Inserting multiple rows
    By Mike in forum Website Programming & Databases
    Replies: 3
    Last Post: 05-13-2004, 11:41 PM
  2. Exchanging Links but not returning their link.
    By iKwak in forum Search Engine Optimization
    Replies: 3
    Last Post: 02-21-2004, 06:20 AM
  3. Excluding Rows in PHP?
    By Mike in forum Website Programming & Databases
    Replies: 18
    Last Post: 01-27-2004, 04:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •