How to fetch the first 2 or 3 rows from Mysql DB using Mysql PHP function?
How to fetch the first two rows from Mysql DB using Mysql PHP function? Is there any function which can give me first 2 or 3 rows from the select qu开发者_Python百科ery we fired?
Use LIMIT
. From the manual, to retrieve 3 rows:
SELECT * FROM tbl LIMIT 3;
Or to retrieve rows 6-15:
SELECT * FROM tbl LIMIT 5,10;
For this query (i.e. with no constraint) if you are not using an ORDER BY
clause your results will be ordered as they appear in the database.
You can use the limit
clause in your query:
select * from your_table limit 3
This will select the first three rows.
And:
select * from your_table limit 5, 3
The later will select rows starting from 5 and return three rows.
You have 2 options:
- Fire a query to select all the rows
and then select 2 or 3 rows as
needed using
PHP
. - Fire a query to select only the
necessary number of rows using
LIMIT
clause.
The 2nd option is preferable.
精彩评论