Limit amount of records shown

I have the simple bit of code that works…

//GET DATA FROM DATABASE
$title_query = mysql_real_escape_string($_POST['query']); 
$result = mysql_query("
   SELECT *
   FROM gallery 
   
");

 

require("main-page.php");



   
//DISPLAY GALLERY ITEMS 
    
  while($row = mysql_fetch_array($result)) 
  {
    echo "<h2>" . $row['title'] . "<br /></b></h2>";
    echo "<p>" .$row['description'] . "<br /></p>";
    echo "<p><b>Step 1:</b> " .$row['step1'] . "<br /></p>";
    echo "<p><b>Step 2:</b> " .$row['step2'] . "<br /></p>";
    echo "<p><b>Step 3:</b> " .$row['step3'] . "<br /></p>";
    echo "<p><b>Step 4:</b> " .$row['step4'] . "<br /></p>";
    echo "<p><b>Step 5:</b> " .$row['step5'] . "<br /></p>";
    echo "<p><b>Step 6:</b> " .$row['step6'] . "<br /></p>";
    echo "<p><b>Step 7:</b> " .$row['step7'] . "<br /></p>";
    echo "<p><b>Step 8:</b> " .$row['step8'] . "<br /></p>";
    echo "<p><b>Step 9:</b> " .$row['step9'] . "<br /></p>";
    echo "<p><b>Step 10:</b> " .$row['step10'] . "<br /></p>";
    echo "<p><b>Category:</b> " .$row['maincat'] . "<br /></p>";
    
  }
} 


else {
    echo 'there are no results!'; 

}

This will display every record in the DB table. I want to limit the number of records and display them randomly but I have no idea how to do this. Can anyone help please?

@GOPalmer

“ORDER BY rand()” does the work ? isn’t rand a php method ?

somebody can explain me how does this work ?:confused:

You can just do


$result = mysql_query("

   SELECT *

   FROM gallery 

   ORDER BY rand()
   LIMIT 10
   

");


lol I didn’t knew… I was radomizing with php until now :slight_smile:

RAND() is a MySQL function. Read more here: http://dev.mysql.com/doc/refman/5.1/en/mathematical-functions.html#function_rand

Perfect works for me. Thanks.