How can i fetch only first 5 records in sql using php?

4 replies
Hello people

i am using mysql database with php. I have 1000+ records in my table. I want to fetch only first five records in php page how can i do that?
#fetch #php #records #sql
  • Profile picture of the author skillboyz
    You can use simple sql statement to fetch top 5 records you can use limit clause for it. here is an example of it

    select * from tablename limit 0,5

    hope it helps
    {{ DiscussionBoard.errors[8280300].message }}
  • Profile picture of the author dad2four
    You'll wand do do an order by to be sure you get the 5 you want.

    Also keep in mind you may need ASC or DESC depending on what you are ordering by. Especially if you are ordering by dates.
    Signature
    {{ DiscussionBoard.errors[8282627].message }}
  • Profile picture of the author sktthemes
    Hello
    You can use simple mysql query for displaying first five records
    mysql_query("select * from table_name order by ID ASC LIMIT 0, 5");
    {{ DiscussionBoard.errors[8309566].message }}
  • Profile picture of the author Andrew H
    yet your screen name is programmingking? lol

    SQL Limit - MySQL Limit - SQL Limit Results
    Signature
    "You shouldn't come here and set yourself up as the resident wizard of oz."
    {{ DiscussionBoard.errors[8310978].message }}

Trending Topics