Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP + MySQL - Multi row data return

Options
  • 01-12-2008 12:47pm
    #1
    Registered Users Posts: 386 ✭✭


    Anyone that may know what I'm doing wrong? I have a MySQL DB with a table of data along the lines of:

    ID DeviceID Value1 Value2
    01 Device1 ValueA ValueAA

    02 Device2 ValueB ValueBB

    03 Device3 ValueC ValueCC

    04 Device2 ValueD ValueDD

    What I want to do is through PHP pull data from the table of all rows that hold values for a particular device - for example Device2 which would be row id 2 and 4 (btw, I use the ID column as the index with unique values) and by using below code I would hope to get a output along the lines of

    Data in row: Device2 ValueB ValueBB
    Data in row: Device2 ValueD ValueDD

    [PHP]
    $passed_id = $_GET["passed_id"];

    $query = "SELECT * FROM `tableName` where `deviceID` = $passed_id";
    $result = mysql_query($query);

    $row = mysql_fetch_array($result, MYSQL_NUM);

    while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
    echo 'Data in row: '$row[1],' ',$row[2]' ',$row[3]; ?> <br /> <?
    };
    [/PHP]

    However, all I get is
    Data in row: Device2 ValueD ValueDD

    eg. I only get the last record of that device that is in the database...

    This is driving me nuts and I have a feeling that it's something quite simple I'm missing? :(

    I would appreciate any thoughts on this.

    BTW, I should say that when using the "SELECT * FROM `tableName` where `deviceID` = $passed_id" query through the likes of PHPMyAdmin it does return the data alright.


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    The function mysql_fetch_array() gets the next row of data and then increments the pointer on to the row after that.

    In your code, you're retrieving the first row of data, ignoring it, then retrieving the second row of data (at the start of the while loop) and outputting that.

    Remove the
    $row = mysql_fetch_array($result, MYSQL_NUM);
    Before the while loop.


  • Registered Users Posts: 386 ✭✭JanneG


    Thanks a million!

    That was the exact issue... And like I said... I knew it had to be something simple enough... ;)


Advertisement