Call Random Row from a Database

Discussion in 'PHP' started by carl_in_florida, Aug 20, 2007.

  1. #1
    I have a script where I am calling from a random row but it is only displaying selections from the first couple hundred rows. Here is the text:

    Any Ideas?

    Is there a better way to do this?
     
    carl_in_florida, Aug 20, 2007 IP
  2. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This will give you a single random row from $table:

    
    <?php
    
    $result = mysql_query('select * from ' . $table . ' order by rand() limit 1');
    
    ?>
    
    PHP:
    If you need more than a single random row, change limit 1 to however many random rows you want.
     
    sea otter, Aug 20, 2007 IP
    carl_in_florida likes this.
  3. carl_in_florida

    carl_in_florida Active Member

    Messages:
    1,066
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    90
    #3
    That even loads faster. Don't know where I came up with that complicated script I had.

    Green to you
     
    carl_in_florida, Aug 21, 2007 IP
  4. sea otter

    sea otter Peon

    Messages:
    250
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Glad to help :)
     
    sea otter, Aug 21, 2007 IP