MySQL Limit Data
LIMIT restricts the number of rows returned by a SELECT query.
What is it?
LIMIT restricts the number of rows returned by a SELECT query.
Why do we use it?
LIMIT is useful for dashboards, recent records and pagination.
How does it work?
MySQL stops returning rows after the specified limit.
Syntax
SELECT * FROM students LIMIT 10;
Example
<?php
$conn = new mysqli("localhost", "root", "", "aifa_php_lab");
$result = $conn->query("SELECT id,name,email FROM students ORDER BY id DESC LIMIT 10");
while ($row = $result->fetch_assoc()) {
echo htmlspecialchars($row['name']) . "<br>";
}
?>
Example Explained
More Examples
LIMIT 10 OFFSET 10 can be used as part of a pagination strategy.
⚠️ Important Rules
Use ORDER BY when the meaning of the first records matters.
❌ Common Mistakes
Using LIMIT without sorting when you need the newest or oldest records.
🌍 Real-World Use
Pagination, recent students, latest orders and dashboard widgets.
📝 Practice Exercise
Create a Latest 10 Students section.
💻 Try It Yourself
Write and run this PHP lesson inside the AIFA Coding Lab.