PHP & MySQL | Tutorials

David Carr

Limit MySQL output

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. 

Domains are often purchased from multiple providers, keeping track of where a domain is and its DNS settings can be tricky. Domain Mapper solves this by listing all your domains in one place. View your DNS settings and receive reminders to renew your domains. Try it today.

Support my work by donating with PayPal.

Subscribe to my newsletter

Subscribe and get my books and product announcements.

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