pdo - JSON response not as expected using php -
i know simple question, can't find answer issue, maybe because basic php programming question. function using pdo (php):
<?php function getallusers(){ try { $conn = getconnection(); //connects database, no explanation needed... uses pdo $dbh = $conn->prepare("select * user;"); $dbh->execute(); $users = $dbh->fetchall(); //<---this maybe error $conn = null; return $users; }catch (pdoexception $ex){ echo "error: ".$ex->getmessage(); } } ?>
and when consume api i'm implementing, use other php script (using slim framework, still pretty understandable)
<?php $app->get("/user",function() use($app){ $app->response->headers->set("content-type","application/json"); $app->response->status(200); $result = getallusers(); //call function getallusers $app->response->body(json_encode($result)); }); ?>
it works fine, results these:
[{"iduser":"1","0":"1","username":"asdasd","1":"asdasd","userpass":"password","2":"password"},{"iduser":"2","0":"2","username":"2312","1":"2312","userpass":"password","2":"password"}]
and think repeated values "0":"1" , "1":"asdasd" , "2":"password"
should not there, can't figure out how data want , not repeated values. appreciated
i'm not using pdo, "common" mysql queries , there same...you doubled values. 1 array associative , other indexed (0,1,2). , there option pass ("mysql_assoc" if recall well) associative array, without indexed one. there must similar option pdo.
$dbh->fetchall(pdo::fetch_assoc);
Comments
Post a Comment