php - output issue with pdo::fetchall -
source code:
<?php $dsn = 'mysql:dbname=oop;host=localhost;'; $user = 'admin'; $password = 'password'; try { $pdo = new pdo($dsn, $user, $password); } catch (pdoexception $e) { echo 'connection failed: ' . $e->getmessage(); } $stmt = $pdo->prepare("select * be_users"); $stmt->execute(); echo "<pre>"; print_r($stmt->fetchall()); echo "</pre>"; ?> result:
array ( [0] => array ( [username] => tom [0] => tom [email] => dsfde@gmail.com [1] => dsfde@gmail.com ) [1] => array ( [username] => tom3 [0] => tom3 [email] => sdfsdfs@gmail.com [1] => sdfsdfs@gmail.com ) ... question:
why result not this:
array ( [0] => array ( [username] => tom [email] => dsfde@gmail.com ) [1] => array ( [username] => tom3 [email] => sdfsdfs@gmail.com )
you using default fetch style, pdo::fetch_both, meaning array indexed both numerical , associative. use pdo::fetch_assoc instead:
print_r($stmt->fetchall(pdo::fetch_assoc)); you'll find more information fetch styles in manual page of pdostatement::fetchall()
note can specify fetch mode per statement or globally per pdo instance:
$pdo->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_assoc);
Comments
Post a Comment