Reputation: 416
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?
Upvotes: 0
Views: 41
Reputation: 26258
Change the following line:
echo $row['ID'];
to
$val = $row['ID'];
on each iteration $val will have new value in it.
Upvotes: 0
Reputation: 1333
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);
Upvotes: 2