<?php
require 'dbinfo.php';
try {
$db = new PDO($dsn, $username, $password);
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth = $db->query("SELECT * FROM user_tracks");
$loc = $sth->fetchAll();
$locations = array_values($loc);
echo json_encode( array("user"=>( $locations )));
} catch (Exception $e) {
echo $e->getMessage();
}
?>
代码应返回:
{"user":[{"id":"1","Latitude":"12.9555033333","Longitude":"80.2461883333","Time":"06:32:57","Date":"2012-03-13","Speed":"0","Course":"183.92"},{...},{....}]}
返回时:
{"user":[{"id":"1","0":"1","Latitude":"12.9555033333","1":"12.9555033333","Longitude":"80.2461883333","2":"80.2461883333","Time":"06:32:57","3":"06:32:57","Date":"2012-03-13","4":"2012-03-13","Speed":"0","5":"0","Course":"183.92","6":"183.92"},{...},{....}]}
我不确定发生了什么......这里的问题在哪里?
提前致谢!
答案 0 :(得分:8)
fetchAll()returns both(请注意'fetch_style'参数注释/注释)字符串和默认情况下来自查询结果的数字键控数据。如果只需要字符串版本,则必须执行
$loc = $sth->fetchAll( PDO::FETCH_CLASS );