1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Php MYSQL Select Random

Discussion in 'PHP' started by AndyFarrell, Apr 20, 2010.

  1. #1
    hey everyone, again i am very new to php but i am learning very fast. Asp.net is still my baby but i love php. Anyhow i have a little problem.Is there a way to make this more random ?

    $result = mysql_query('SELECT * FROM pictures ORDER BY RAND() LIMIT 1');
     
    AndyFarrell, Apr 20, 2010 IP
  2. tguillea

    tguillea Active Member

    Messages:
    229
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    90
    #2
    
    $result = mysql_query("SELECT * FROM `pictures` ORDER BY `id` DESC") //replace id with your primary key
    $x = 0;
    while ($row = mysql_fetch_array($result)){
        $ids[$x] = $row['id'];
        $x++;
        }
    asort($ids);
    $random_id = rand($ids[0],$ids[(count($ids) - 1)]);
    $final_result = mysql_query("SELECT * FROM `pictures` WHERE `id`='$random_id'");
    
    PHP:

    Edit: I think I got this mixed up, did you want to display all the pictures in a random order?

    According to (http://bugs.mysql.com/bug.php?id=817), this bug has been fixed. Have you tried updating your MySQL?
     
    tguillea, Apr 20, 2010 IP
  3. lukeg32

    lukeg32 Peon

    Messages:
    645
    Likes Received:
    19
    Best Answers:
    1
    Trophy Points:
    0
    #3
    Using rand() is very slow and will only get slower as your database grows.

    What do you mean "more random"? There are a multitude of ways of fetching a random row from a database, a few examples are here;

    http://akinas.com/pages/en/blog/mysql_random_row/

    You can also process the randomization through PHP rather than the database itself.
     
    lukeg32, Apr 21, 2010 IP
  4. HiMyNameIsErez

    HiMyNameIsErez Greenhorn

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    If your database is small,you should use RAND() otherwise try another solutions as like mentiones in the link that lukeg gave you
     
    HiMyNameIsErez, Apr 21, 2010 IP
  5. NemoPlus

    NemoPlus Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Dear Andy,

    Me too, I would process the randomization through PHP: you can
    - first retrieve the rows from DB
    - then use the prebuilt shuffle function from PHP: php.net/manual/en/function.shuffle.php

    Good luck
     
    NemoPlus, Apr 21, 2010 IP
  6. superscript

    superscript Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hi there,

    I'm looking for something similar, I want to

    select a random row and update an specific field.

    say tbl_images

    and fld_show is = by default, if set it show=1; the image shows.

    so I want to select a random row and enable 1 pic every time I run the page.
     
    superscript, Nov 28, 2010 IP
  7. socalcoder

    socalcoder Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    My experience with rand under mysql is awful.
    Depending on the size of your table and your indexes, the result can just be one loooonnngg wait until you have the result back.
    If your table has an ID field (auto increment primary key type thing), I would choose a randon number under php and call that number out.
    Pseudo code would be like :

    $number = rand(0,10000);
    $sql = "select * from whatever where id='$number'";
    $result = mysql_query...etc
    PHP:
     
    Last edited: Nov 29, 2010
    socalcoder, Nov 29, 2010 IP
  8. nadeem3366

    nadeem3366 Member

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #8
    hi,

    socalcoder answer is the best one answer for your Question AndyFarrell ! and after where condition you can also limit your records, so have you tried it ?
     
    nadeem3366, Nov 29, 2010 IP