PHP MySQL Limit Data
Controlling the number of records returned from database
📊 What is MySQL LIMIT?
The LIMIT clause restricts the number of rows returned by a SELECT query. It's essential for pagination and improving performance when working with large datasets.
<?php
// Get only 5 records
$sql = "SELECT * FROM users LIMIT 5";
$result = mysqli_query($conn, $sql);
?>
LIMIT Usage Patterns
Basic Limit
Get first N records
SELECT * FROM users LIMIT 10
Offset
Skip records and then limit
SELECT * FROM users LIMIT 5, 10
Pagination
Display data in pages
LIMIT $start, $per_page
Top Results
Get highest/lowest records
ORDER BY score DESC LIMIT 3
🔹 Basic LIMIT Clause
The simplest form of LIMIT returns a specified number of records from the beginning of the result set:
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
// Get first 5 users
$sql = "SELECT id, name, email FROM users LIMIT 5";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
}
} else {
echo "No records found";
}
mysqli_close($conn);
?>
Output:
ID: 1 - Name: John Doe
ID: 2 - Name: Jane Smith
ID: 3 - Name: Bob Johnson
ID: 4 - Name: Alice Brown
ID: 5 - Name: Charlie Wilson
🔹 LIMIT with OFFSET
Use OFFSET to skip a certain number of records before limiting. Syntax: LIMIT offset, count or LIMIT count OFFSET offset:
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
// Skip first 3 records, then get 5 records
$sql = "SELECT id, name FROM users LIMIT 3, 5";
// Alternative syntax: LIMIT 5 OFFSET 3
$result = mysqli_query($conn, $sql);
echo "Records 4-8:<br>";
while($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["id"] . " - " . $row["name"] . "<br>";
}
mysqli_close($conn);
?>
Output:
Records 4-8:
ID: 4 - Alice Brown
ID: 5 - Charlie Wilson
ID: 6 - David Lee
ID: 7 - Emma Davis
ID: 8 - Frank Miller
🔹 Pagination Example
LIMIT is commonly used for pagination, dividing large result sets into manageable pages for better user experience:
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
// Pagination settings
$records_per_page = 10;
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$start_from = ($page - 1) * $records_per_page;
// Get records for current page
$sql = "SELECT id, name, email FROM users LIMIT $start_from, $records_per_page";
$result = mysqli_query($conn, $sql);
// Display records
while($row = mysqli_fetch_assoc($result)) {
echo $row['name'] . " - " . $row['email'] . "<br>";
}
// Get total records for pagination links
$sql_count = "SELECT COUNT(*) as total FROM users";
$result_count = mysqli_query($conn, $sql_count);
$row_count = mysqli_fetch_assoc($result_count);
$total_pages = ceil($row_count['total'] / $records_per_page);
// Display pagination links
echo "<br>Pages: ";
for($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=$i'>$i</a> ";
}
mysqli_close($conn);
?>
Output:
🔹 Top N Results with ORDER BY
Combine LIMIT with ORDER BY to get top or bottom records based on specific criteria:
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
// Get top 3 highest scores
$sql = "SELECT name, score FROM students
ORDER BY score DESC
LIMIT 3";
$result = mysqli_query($conn, $sql);
echo "Top 3 Students:<br>";
$rank = 1;
while($row = mysqli_fetch_assoc($result)) {
echo $rank . ". " . $row['name'] . " - Score: " . $row['score'] . "<br>";
$rank++;
}
mysqli_close($conn);
?>
Output:
Top 3 Students:
1. Alice Brown - Score: 98
2. John Doe - Score: 95
3. Emma Davis - Score: 92
🔹 Dynamic LIMIT with Variables
Use variables to make LIMIT values dynamic and flexible:
<?php
$conn = mysqli_connect("localhost", "root", "", "myDB");
// Dynamic limit values
$limit = 5;
$offset = 10;
// Using prepared statement for security
$stmt = $conn->prepare("SELECT id, name FROM users LIMIT ?, ?");
$stmt->bind_param("ii", $offset, $limit);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()) {
echo $row['name'] . "<br>";
}
$stmt->close();
mysqli_close($conn);
?>
🔹 Important Tips
💡 Best Practices:
- Performance: LIMIT improves query speed on large tables
- Pagination: Always calculate offset as (page - 1) × records_per_page
- Order matters: Use ORDER BY before LIMIT for consistent results
- Count total: Use COUNT(*) separately to get total records for pagination
- Validation: Always validate page numbers to prevent errors