Derple
Derple

Reputation: 863

SELECT DISTINCT rows

I am trying to output each of the rows by the value driver_profileId.

Currently this only outputs one value to the page. I am expecting a list of all the unique values in the cell.

Why is this not outputting more than one row?

$query = "SELECT DISTINCT(driver_profileId) FROM driver_profiles";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
foreach ($row as $key) {
    echo $key;      
}

Upvotes: 1

Views: 30

Answers (1)

Jeff Lambert
Jeff Lambert

Reputation: 24661

I believe your issue is because you're only fetching one row. Try something more like this:

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo $row['driver_profileId'];
}

This will keep fetching rows until there are no more in the result set. See the manual page for mysqli_fetch_array:

Returns an array that corresponds to the fetched row or NULL if there are no more rows for the resultset represented by the result parameter.

Upvotes: 2

Related Questions