gpt4 book ai didi

php - JSON 数据格式不正确 : fetching from mysql

转载 作者:可可西里 更新时间:2023-11-01 07:50:02 25 4
gpt4 key购买 nike

<?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"},{...},{....}]}

我不确定发生了什么......这里的问题在哪里?

提前致谢!

最佳答案

fetchAll() returns both (注意 'fetch_style' 参数注释/注释)默认情况下来自查询结果的字符串和数字键数据。如果你只想要字符串版本,你必须这样做

$loc = $sth->fetchAll( PDO::FETCH_CLASS );

关于php - JSON 数据格式不正确 : fetching from mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com