Brayn Brayn - 10 months ago 45
PHP Question

Seperate ID's resulted from while query

I have a query that returns all information from a specific user in a table. This user can have multiple records in the table and they're uniquely identified by an

ID
column.

$tsql = "SELECT * FROM Table WHERE UserID = '$UserID'";
$stmt = sqlsrv_query( $conn, $tsql);
while($row = sqlsrv_fetch_array($stmt)) {
echo $row['ID'];
}


This returns 949596 which are the three ID's 94, 95 and 96 in the DB. How can I separate that result and have each ID in a variable?

To get the last ID I can do:

while($row = sqlsrv_fetch_array($stmt)) {
$AssignedID = $row['ID'];
}

echo $AssignedID;


This would result in 96 which is the last assigned ID. Unfortunately it's not enough as I need all of them in order to allow the end user to update their objects through a page. This all depends on the ID's

I tried working with an array like someone suggested, but just can't seem to get it working.

Anyone who could help me?

Answer Source

you can just put all of them in a array() and print them out...

$ids = array();
$tsql = "SELECT * FROM Table WHERE UserID = '$UserID'";
    $stmt = sqlsrv_query( $conn, $tsql);
    while($row = sqlsrv_fetch_array($stmt)) {
        $ids[] = $row['ID'];
    }
print_r($ids);