I'm sure I must be doing something wrong, but I can't seem to find an answer anywhere (at least neither in the documentation nor on SO as of yet). I am a beginner in PHP and SQL.
I have this simple table called "mObject_has_media" with only two columns "media_idMedia" and "mObject_idmObject"
When I try to query the table with this code,
public function selectMediaIds (MObject $object) {
$medias = array ();
$req = $this -> _db -> query ('SELECT media_idMedia FROM mObject_has_media WHERE mObject_idmObject = ' . $object -> id ());
while ($data = $req -> fetch (PDO::FETCH_ASSOC)) {
$medias[] = $data;
}
$req -> closeCursor ();
return $medias;
}
the problem is this: when I var_dump the contents of the method's result, I get:
array (size=2)
0 =>
array (size=1)
'media_idMedia' => string '52' (length=2)
1 =>
array (size=1)
'media_idMedia' => string '53' (length=2)
I'd like to get rid of those nested arrays and just get something like:
array (size=2)
0 => 'media_idMedia' => string '52' (length=2)
1 => 'media_idMedia' => string '53' (length=2)
so I can simply run this array into a foreach loop right afterwards... I'm sure that this problem has everything to do with the fetch() method, but in spite of experimenting with different options, I haven't been able to get the desired behavior.
A similar question (I think) was asked here: PDO::FETCH_ASSOC Does not return the correct table columns but the answer mentioned the Zend framework, and I am NOT using any framework at all, this is just vanilla PHP.
0 => 'media_idMedia' => string '52' (length=2)makes no sense. That isn't a real data structure, but the closest thing it resembles is a nested array, which you claim to be trying to avoid. You can have a string key, or a numeric key, but you can't have a numeric key pointing at a string key pointing at a value. That doesn't make sense.