Search code examples
phpmysqlpdogroup-bydistinct

group by not showing all results


So i have a table called "watches"

then i have 3 columns like so

id | manufacture | model
1    rolex         2483
2    rolex         3940
3    rolex         4940
4    dent          ee30

but i want the output to be like

rolex
dent

But when i try the following to select the manufactures

('SELECT manufacture FROM `watches` GROUP BY manufacture')

at the moments its only showing "dent" when it should be "rolex, dent". How come its not working. iv also tried this, but this gave the same result

('SELECT DISTINCT(`manufacture`) FROM `watches`')

full function

$query = $this->pdo->prepare('SELECT DISTINCT(`manufacture`) FROM `watches`');

$query->execute();

if($query->fetch()){

    $row = $query->fetchAll();
    foreach ($row as $i) {
        $select .= '<option value="'.$i['manufacture'].'">'.$i['manufacture'].'</option>';
    }
    return $select;
} else {
    return '<option value="0">No watches yet!</option>';
}

Solution

  • Please try to use

    if ($query->rowCount() > 0) {
    

    instead of

    if($query->fetch()){
    

    because fetch gets the result from statement but for this you use fetchAll in the successing code.