php: why is $row[0] not returning anything?

I am converting all my php scripts due to moving to a new server. I am stumped as to why $row[0] is not working.

I am correctly getting $row populated as an array, and if I run a foreach on it, I get all the values populated just fine. But if, instead, I try to directly access the first value of the array as $row[0], I get nothing. Anyone know what?

$result = $dbh->query($sql);
while($row = $result->fetch_assoc()) {
        print $row[0]; // this prints nothing
        foreach($row as $column) {
            print "$column"; // this works as expected
} #<-- while

>Solution :

fetch_assoc returns your results as a key value of column names and values, so you need to look at the $row['myColumn'] to get the value.

Leave a Reply