Limit MySQL output

David Carr

Limit MySQL output

PHP & MySQL Tutorials

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. 

Laravel Modules Book by David Carr

Help support the blog so that I can continue creating new content!

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2009 - 2022 DC Blog. All code MIT license. All rights reserved.