Select (5) Rows from MySQL Database - Into Array

Discussion in 'PHP' started by LindseyInteractive, Dec 31, 2011.

  1. #1
    I am looking to fetch 5 Rows from a database and then each row into an array, but I am not sure how to do it, so I can access all 5 rows from the array variable.
     
    LindseyInteractive, Dec 31, 2011 IP
  2. Wasi88

    Wasi88 Active Member

    Messages:
    56
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    55
    #2
    Add a limit value to your sql statement to get just a specific count from your items.
    Something like this: SELECT * FROM `your_table` LIMIT 0, 5
    This sql statement selects the first 5 items from your table.

    Then it's up to the programming language you use to get the items from your database table.
    In PHP you can use mysql_query() and mysql_fetch_array() to get the items and iterate through:
    $query = "SELECT * FROM 'your_table' LIMIT 0, 5";
    $result = mysql_query($query) or die('Query: '.$query.'<br>Failed: '.mysql_error());
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { /* ... */ }


    Hope it helps.
     
    Wasi88, Dec 31, 2011 IP