I know how to fetch a PDO array, but how do I collect data from it like you do with MySQLi's fetch_array?
For example,
MySQLi
$query = $mysqli->query("SELECT * FROM `foo` WHERE `ID`='1'");
$array = $query->fetch_array();
Getting a result
echo $array['bar'];
How would you do this with PDO? I understand you can do this:
PDO
$query = $pdo->prepare("SELECT * FROM `foo` WHERE `ID`='1'");
$query->execute();
$result = $query->fetchAll();
Getting the result
echo $result['bar'];
Does not return the same as MySQLi did
Am I doing something wrong, and is there a way of doing this?
fetch_styleparameter php.net/manual/en/pdostatement.fetch.phpPDO::FETCH_ASSOCreturns an array indexed by column name as returned in your result setfetch_array()iterates over the whole result set and returns one row at a time,fetchAll()returns the whole result set. Do this instead:$query->fetch(PDO::FETCH_ASSOC)print_r()and/orvar_dump()to divine the often poorly-documented data structures PHP throws at you.