Currently seeking new career opportunities in web development, particularly with Laravel, Hire Me

Limit MySQL output

David Carr

Tutorials PHP & MySQL

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 Your Logo Your Logo Your Logo

Become a sponsor

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

Sponsor

My Latest Book

Modular Laravel Book - Laravel: The Modular way

Learn how to build modular applications with Laravel Find out more

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Learn Laravel with Laracasts

Faster Laravel Hosting

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.