Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch the first 2 or 3 rows from Mysql DB using Mysql PHP function?

Tags:

php

mysql

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 query we fired?

like image 729
OM The Eternity Avatar asked Dec 28 '22 17:12

OM The Eternity


2 Answers

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.

like image 109
Andy Avatar answered Dec 31 '22 07:12

Andy


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.

like image 25
Sarfraz Avatar answered Dec 31 '22 07:12

Sarfraz