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.
| SitePoint Sponsor |


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.


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_all= mysql_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_ran= mysql_query("SELECT * FROM advertisements where id='$c1'",$db);
while ($myrow = mysql_fetch_array($pick_ran)){
echo"<pre>$myrow[description]</pre><BR>";
}
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.





SELECT * FROM advertisements ORDER BY RAND( ) LIMIT 1
Matt - Sybase DBA / PHP fanatic
Sybase/MySQL/Oracle | I don't like MySQL
Download Sybase | DBForums.com - for all your RDBMS talk
Bookmarks