Log in

View Full Version : Limit MySQL Results



alexjewell
05-26-2010, 09:31 PM
I'm connecting to a database and pulling results by the date they were added and sorting them accordingly, along with name. This explains the following code:



$result = mysql_query("SELECT * FROM standFields ORDER BY dateadded DESC, name ASC");
echo '<br /><span class="errMsg indent emSmall">Currently sorting by <i style="color:darkred;">date added</i></span><br />'."\n";
echo '<table cellpadding="5px" cellspacing="0px" class="tableBorder indent"><tr>'."\n";
unset($standFields['notes']);
foreach($standFields as $key => $value){ echo "<td><b>$value</b></td>";}
echo "\n".'</tr>'."\n";
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
echo '<tr>';
foreach($standFields as $key => $value){
if($key == 'name'){ echo '<td><a href="dEdit.php?viewSN='.$row['sn'].'">'.$row['name'].'</a></td>';}
else if($key == 'tags'){ echo '<td><a href="dEdit.php?viewSN='.$row['sn'].'">'.$row['tags'].'</a></td>';}
else{ echo "<td>$row[$key]</td>";}
}
echo '</tr>';
}
echo '</table>';


My question is this: how can I alter that while loop to only include, say, the most recent 20 rows, rather than all of them? There are going to be a huge amount of rows and it'd be nice to limit how many are displayed as the most recently added. Thanks.

djr33
05-26-2010, 09:58 PM
[query].... ORDER BY `field` DESC LIMIT 20;

alexjewell
05-27-2010, 04:05 AM
Oh, well that's much simpler than I thought it'd be. Haha. Thanks!