how can i print a returned array's multiple results

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rotaryfreak
    New Member
    • Oct 2008
    • 74

    #1

    how can i print a returned array's multiple results

    Hi,


    in my database table, i have inserted the following data after creating the appropriate tables:


    Code:
    insert into Transaction values(0001, null, 'BransenEnt', 1, 'Mastercard', 99.99, '2010-06-05', '10:00:00');
    
    insert into Transaction values(0002, 'blackbear62', null, null, 'Visa', 29.99, '2010-01-03', '13:47:00');
    
    insert into Transaction values(0003, null, 'JoeRod', 2, 'Visa', 99.99, '2009-07-22', '17:15:00');
    im building section of an application which needs to check for all transactions between two dates. I do this by:

    Code:
    $query = "SELECT * FROM Transaction WHERE Date between \"2009-06-12\" AND \"2010-07-05\" ";
    When i run this straight from the mysql command line, i get the 3 above results. When i run the same query in my application, with some added sql functions:

    Code:
    $query = "SELECT * FROM Transaction WHERE Date between \"2009-06-12\" AND \"2010-07-05\" ";
    
    $result = mysql_query($query);
    $array = mysql_fetch_assoc($result);
    print_r($array);
    i only get the first result. Im pretty sure that it has to do with the array being returned only having 1 row so how do i use PHP to print out the other rows?

    i havent been very successful with google :(

    thank you
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    The reason is
    mysql_fetch_ass oc($result)
    always return the first un-accessed row.

    If you want to get through the all row you need to do it until mysql_fetch_ass oc return null. Check the below code.

    Code:
    while($array=mysql_fetch_assoc($result))
    {
     print_r($array);
    }

    Comment

    • rotaryfreak
      New Member
      • Oct 2008
      • 74

      #3
      awesome, thanks a lot!

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        you can also try PDO's or MySQLi's fetchAll() method

        Comment

        Working...