SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pick Random From DB

    Does anyone have the code to just get a random row from a database? I have a advertisements database and I need for it to display a different one everytime.

  2. #2
    SitePoint Addict manipura's Avatar
    Join Date
    Apr 2001
    Location
    Calgary,AB
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    FOUND IT!

    Nevermind, I figured it out, wasn't nearly as hard as I had thought. Altough if you have a better code then me, please I would love to get it from you

    PHP Code:
    $one = array("1");

    $pick_allmysql_query("SELECT * FROM advertisements where id !=1",$db);
    while (
    $myrow mysql_fetch_array($pick_all)){
    $adverid $myrow["id"];
    array_push($one,"$adverid");
       
    }
    $max_elements count($one) - 1
    srand((double)microtime()*1000000);
    $c1 $one[rand(0,$max_elements)];  

    echo
    "$c1";


    $pick_ranmysql_query("SELECT * FROM advertisements where id='$c1'",$db);
    while (
    $myrow mysql_fetch_array($pick_ran)){

    echo
    "<pre>$myrow[description]</pre><BR>";


  3. #3
    SitePoint Enthusiast poil11's Avatar
    Join Date
    Aug 2000
    Location
    it look me along time to do that...
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT *, ID*0+RAND() LIMIT 1
    I will Tell you what is wrong with your website and maybe what you can do to make it better.
    Professional Web Site Reviews ... Usability ...
    Contact Me god@stillsucks.com for more information ...
    look at my other reviews, to see what i have done.

  4. #4
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT * FROM advertisements ORDER BY RAND( ) LIMIT 1


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
  •