how to select random data from table?

Codes here !

Moderators: egami, macek, gesf

Post Reply
User avatar
Joan Garnet
Moderator
Moderator
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars
Contact:

Thu Nov 21, 2002 4:53 am

You can use the rand function combined with a query:

Code: Select all

<?php 
$sql="select my_column from my_table'";
$result=mysql_query($sql,$db);
while ($row=mysql_fetch_array($result)){
      $total_num_rows = mysql_num_rows ( $result );
      $rand = ( rand ( 0, $total_num_rows ) );
      $random_data = $row [$rand];
      echo $random_data;
}
?>


:)

Oleg Butuzov
Last Samuray
Last Samuray
Posts: 824
Joined: Sun Jun 02, 2002 3:09 am

Sat Nov 23, 2002 10:10 am

selecrt * from sometable order by rand() limit 1

Oleg Butuzov
Last Samuray
Last Samuray
Posts: 824
Joined: Sun Jun 02, 2002 3:09 am

Thu Dec 12, 2002 12:39 am

$n=10; //num of recordes

selecrt `yourrows` from sometable order by rand() limit $n

or use a few querys =)

Post Reply
  • Information
  • Who is online

    Users browsing this forum: Baidu [Spider] and 1 guest