PHP -Mysql query help required

5 replies
I have many records in the table I want to fetch only first 5 records from the table using query of mysql and print into php. Can anyone tell me how can i do it?
#mysql #php #query #required
  • Profile picture of the author Andrew H
    SELECT * FROM yourtable LIMIT 0,5

    http://dev.mysql.com/doc/refman/5.0/en/select.html
    Signature
    "You shouldn't come here and set yourself up as the resident wizard of oz."
    {{ DiscussionBoard.errors[8531263].message }}
  • Profile picture of the author JohnMckay
    This is the complete php code which you can run. Create a blank php file and paste this code:

    <?php
    $dbhost = 'YOUR DB HOST HERE'; /* mostly "localhost" */
    $dbname = 'YOUR DB NAME HERE';
    $dbuser = 'YOUR DB USERNAME HERE';
    $dbpass = 'YOUR DB PASSWORD HERE';

    $con = mysql_connect($dbhost, $dbname, $dbpass) or die("Database connection error.");

    if($con){
    mysql_select_db($dbname) or die("Invalid Database");
    }

    $tablename = "YOUR TABLENAME HERE";
    $data = array();

    $query = "SELECT * FROM $tablename LIMIT 0,5";
    $result = mysql_query($query) or die("Invalid SQL Query");

    if(mysql_num_rows($result)){
    while($row = mysql_fetch_assoc($result)){
    array_push($data, $row);
    }
    }

    echo '<pre>';
    print_r($data); /* Here your data in $data array */
    echo '</pre>';
    {{ DiscussionBoard.errors[8533098].message }}
    • Profile picture of the author sonzoy
      Originally Posted by JohnMckay View Post

      This is the complete php code which you can run. Create a blank php file and paste this code:

      <?php
      = 'YOUR DB HOST HERE'; /* mostly "localhost" */
      = 'YOUR DB NAME HERE';
      = 'YOUR DB USERNAME HERE';
      = 'YOUR DB PASSWORD HERE';

      = mysql_connect(, , ) or die("Database connection error.");

      if(){
      mysql_select_db() or die("Invalid Database");
      }

      = "YOUR TABLENAME HERE";
      = array();

      = "SELECT * FROM LIMIT 0,5";
      = mysql_query() or die("Invalid SQL Query");

      if(mysql_num_rows()){
      while( = mysql_fetch_assoc()){
      array_push(, );
      }
      }

      echo '<pre>';
      print_r(); /* Here your data in array */
      echo '</pre>';
      Great tutorial but use mysqli instead of old mysql
      {{ DiscussionBoard.errors[8554028].message }}
  • Profile picture of the author stonecoldmf
    You can just use sql query for it select * from tablename limit 0,5 where 0 is start index and 5 is number of records then you can print in php using mysql_fetch_array , make an array and in the loop you can print each field using echo
    {{ DiscussionBoard.errors[8537629].message }}
  • Profile picture of the author stellajohn
    Using LIMIT option we can filter the top first 5 records. Limit is used to limit your MySQL query results to those that fall within a specified range. You can use it to show the first X number of results, or to show a range from X - Y results. It is phrased as Limit X, Y and included at the end of your query. X is the starting point (remember the first record is 0) and Y is the duration (how many records to display). Also Known As: Range Results Examples:
    SELECT * FROM `your_table` LIMIT 0, 5
    Signature

    Best Web Hosting Provider in India | Best Shared Web Hosting in India | Best VPS Hosting in India-http://www.ihostingreviews.in

    {{ DiscussionBoard.errors[8551599].message }}

Trending Topics