Results 1 to 6 of 6

Thread: Getting random rows from a database!

  1. #1
    Join Date
    May 2007
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting random rows from a database!

    I have like 100's of entries in a database
    i was wondering if i could get like a random 3 results from the database?

    Let me know.
    Thanks

  2. #2
    Join Date
    Sep 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Maby something like this?

    PHP Code:
    <?php
        $result 
    mysql_query("SELECT id, name FROM mytable");

        
    $row mysql_fetch_array($resultMYSQL_NUM)) {
        
        
    $num count($row);
        
        
    $random1 rand(1$num);
        
    $random2 rand(1$num);
        
    $random3 rand(1$num);
        
        echo 
    $row[$random1]; 
        echo 
    $row[$random2]; 
        echo 
    $row[$random3];

        
    mysql_free_result($result);
    ?>

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    yes that is possible but you need to describe the fields in the database table.
    the easiest would be to have an "id" for each record stored, and you create a script that will randomly select a number in the range then you would pull the info from that data, and repeat 2 more times
    Last edited by boogyman; 09-05-2007 at 05:36 PM. Reason: // sorry I am a slow typer... elwinh beat me to it

  4. #4
    Join Date
    Sep 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    isn't that almost what the script posted above does? It only dous'nt need a id. It uses "MYSQL_NUM".

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by elwinh View Post
    Maby something like this?

    PHP Code:
    <?php

        $result 
    mysql_query("SELECT id, name FROM mytable");

        
    $row mysql_fetch_array($resultMYSQL_NUM)) {
        
        
    $num count($row);
        
        
    $random1 rand(1$num);
        
    $random2 rand(1$num);
        
    $random3 rand(1$num);
        
        echo 
    $row[$random1]; 
        echo 
    $row[$random2]; 
        echo 
    $row[$random3];

        
    mysql_free_result($result);
    ?>

    rather than declaring each variable explicitly, you could also do a loop to make the code more generalized and save on debug / updating

    PHP Code:
    <?php
        
    // Edit Query to specify what you wish to gather from the database
        
    $result mysql_query("SELECT field(s) FROM mytable WHERE condition");

        
    $row mysql_fetch_array($resultMYSQL_NUM)) {
        
    $num count($row);
        
            
    $int 3;  //Number of Random Datacollections
            
    $random = array();     
            for(
    $count=0$count<$int$count++)
           {
            
    $random[$count] = rand(1$num);
           }

        
    mysql_free_result($result);
        
        
    // Prints out the results
        
    foreach($random as $rand => $val)
        {
                echo 
    "Result "$rand+.": "$val;
         }

    ?>
    would yield

    Result 1: ______something_____
    Result 2: ______something_____
    Result 3: ______something_____
    ...
    Result n+1: _____something_____
    Last edited by boogyman; 09-05-2007 at 05:51 PM. Reason: added comments

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    There's an easier way:
    Code:
    SELECT * FROM mytable ORDER BY RAND() LIMIT 3;
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •