MySQLi查询LIMIT问题:PHP API分页获取700条记录需求实现
Got it, let's sort out this pagination issue. The core problem here is mixing up how MySQL's LIMIT clause works—let's clarify first, then fix your queries.
How MySQL LIMIT Works
The LIMIT syntax follows LIMIT [offset,] row_count, where:
offset: The number of rows to skip before starting to fetch records (starts counting from 0)row_count: The total number of records you want to return
Since you need exactly 700 records per API, row_count will always be 700 for all three endpoints. The offset is what changes based on which page of data you're fetching.
Corrected SQL Queries
Here's the fixed code for each API, with explanations:
API 1 (Fetch records 1–700)
We skip 0 rows and fetch the first 700 records—this one was actually correct already:
$sql = "SELECT number, status, user_id from number_list WHERE disable=0 order by id LIMIT 0,700";
API 2 (Fetch records 701–1400)
We need to skip the first 700 records, then fetch the next 700. Your original query was skipping 701 rows and trying to fetch 1400, which was way off:
$sql = "SELECT number, status, user_id from number_list WHERE disable=0 order by id LIMIT 700,700";
For better readability, you can also use the OFFSET syntax to avoid mixing up parameters:
$sql = "SELECT number, status, user_id from number_list WHERE disable=0 order by id LIMIT 700 OFFSET 700";
API 3 (Fetch records 1401–2100)
Skip the first 1400 records, then fetch the next 700. Your original query was skipping 1401 rows and trying to fetch 2100, which was incorrect:
$sql = "SELECT number, status, user_id from number_list WHERE disable=0 order by id LIMIT 1400,700";
Again, using OFFSET for clarity:
$sql = "SELECT number, status, user_id from number_list WHERE disable=0 order by id LIMIT 700 OFFSET 1400";
Quick Verification
To confirm this works as expected:
- Offset 0 → returns rows 1–700 (700 total)
- Offset 700 → returns rows 701–1400 (700 total)
- Offset 1400 → returns rows 1401–2100 (700 total)
This matches exactly the pagination behavior you need!
内容的提问来源于stack exchange,提问作者Khushi Patel

