Limit MySQL output

David Carr

1 min read - 27th May, 2011

This tutorial will show you how to limit how many results you get from a database query. This tutorial assumes you have an understanding of basic PHP syntax.

When your querying your database you may not always want to see all the results instead just see the last 5 results for example.

This is very simple to do using the LIMIT clause in MySQL.

Here's an example:

$query = "SELECT * FROM table_name WHERE rowID ORDER BY rowID DESC LIMIT 5 ";
$result = mysql_query($query) or die('Error : ' . mysql_error());

This will only show the last 5 results in descending order. By default MySQL outputs results in ascending order so by giving the ORDER BY rowID (name of the row) DESC MySQL will output the results in descending order.

Using LIMIT 5 will tell MySQL to only show 5 results. 

0 comments
Add a comment

Copyright © 2024 DC Blog - All rights reserved.