I'm trying to return a list of course id's from my database with PDO. I can access the data, however, I only want to return an array of unique id's i.e.
This
1,2,2,3,4,4
Becomes
1,2,3,4
I'm using the code below
$sth = $dbh->prepare('SELECT courseId from training');
$sth->execute();
$row = $sth->fetchAll();
$result = array_unique($row);
print_r($result);
But, it returns only 1 id:
Array ( [0] => Array ( [courseId] => 8 [0] => 8 ) )
If I print_r $row I can see all my courseId's
What am I doing wrong?
query()like this:$sth = $dbh->query('SELECT courseId FROM training');. To be more efficient, you can even populate the variable with the result of the query in the same line, like this:$sth = $dbh->query('SELECT courseId FROM training')->fetchAll(PDO::FETCH_ASSOC);.