SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot Anagram's Avatar
    Join Date
    Feb 2003
    Location
    Norway
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A little question about the RAND function in MySQL

    Hi ppl.

    Well, I have a picture database that contains a link to diff. images and a text to each of them. To choose a random picture (that is..a row) from the database, I used this query:

    SELECT * FROM tablename ORDER BY RAND() LIMIT 1

    The problem is that it only selects a certain nr. of rows (for example row 1, 4 and 7) and the effect is that same 3 -three- pictures (i have 6 rows in the database) are shown, and the others are left behind, not showing at all. I mean, I've refreshed like a 100 times, but the S A M E three pictures are shown everytime in a circular fashion.

    If you still wonder what I'm talking about, please visit:

    http://mohclan.servegame.com

    Choose the link "Bilder". In the popup window there's link in the upper left corner that "randomly" chooses another row from the database with the query provided above. You'll soon see the problem.

    I've heard that this is a bug in MYSQL..but I dunno. Please help.
    If pigs could fly, the prize of bacon would reach the sky.

    www.dosspirit.net - Norwegian
    reviews of DOS games

  2. #2
    SitePoint Zealot infoxicated's Avatar
    Join Date
    Jun 2001
    Location
    UK
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to seed the RAND() function.

    Try RAND(NOW()) in order to seed the function and force a more random number.

  3. #3
    SitePoint Zealot Anagram's Avatar
    Join Date
    Feb 2003
    Location
    Norway
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems...

    No, now I get only one row from the database, all the time.

    My query looks like this (after your tip) :

    SELECT * from pictures order by RAND(NOW()) limit 1



    It doesnt work
    If pigs could fly, the prize of bacon would reach the sky.

    www.dosspirit.net - Norwegian
    reviews of DOS games

  4. #4
    SitePoint Zealot infoxicated's Avatar
    Join Date
    Jun 2001
    Location
    UK
    Posts
    140
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird.

    What version of MySQL are you using?

  5. #5
    SitePoint Wizard Sillysoft's Avatar
    Join Date
    May 2002
    Location
    United States :)
    Posts
    1,691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Funny. I have the same issue. Im using mysql 3.23.55 with php 4.3 on iis.

    Silly

  6. #6
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    take a look here: http://www.sitepointforums.com/showt...threadid=93170
    perhaps it helps
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  7. #7
    SitePoint Zealot Anagram's Avatar
    Join Date
    Feb 2003
    Location
    Norway
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL version

    I am using MySQL version 3.23
    If pigs could fly, the prize of bacon would reach the sky.

    www.dosspirit.net - Norwegian
    reviews of DOS games

  8. #8
    SitePoint Addict
    Join Date
    Jul 2002
    Location
    Sooner Nation
    Posts
    208
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    rand?

    this isnt working either
    PHP Code:
    include "dbconnect.inc";
    $results = @mysql_query("SELECT * FROM banners ORDER BY RAND(NOW()) LIMIT 1");
    if (!
    $results) {
        echo(
    "<p>Error locating your username and password " .
          
    mysql_error() . "</p>");
        exit();
      }
    $results mysql_fetch_array($results);
    $id $results["id"];
    $link $results["link"];
    $alt $results["alt"];
    $imagefile $results["imagefile"];
    echo 
    "<a href=""$link target=_blank><img src=""$imagefile alt=$alt border=0></a>"
    All I get is the first result in the database - I hit refresh about 345 times

    using mysql 3.23.54
    any ideas?
    The only function of business is to create customer value and to innovate. - Peter Drucker


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
  •