1

我有一个查询:

    $result = mysql_query("SELECT
    Left(Right(Base.inputs.Data_inf,4),3) As 'Datas',
    count(Base.Data.Data_ID) As `U_Count`
    FROM
        Base.Data
    WHERE
        Base.Data_inputs.info = 'TREE'
    GROUP BY ");

    $rows = array();

    while($r = mysql_fetch_array($result)) {
        $row[0] = $r[0];
        $row[1] = $r[1];
        array_push($rows,$row);
    }

    print json_encode($rows, JSON_NUMERIC_CHECK);

结果是:

[["Data1",38],["Data2",13]]

我希望数据看起来更像:

[{"name":"Data1","data":[38]},{"name":"Data2","data":[13]}]

我试图使用这样的东西:

    $rows = array();
    while($r = mysql_fetch_array($sth)) {
       $rows['name'] = $r['Datas'];
       while($r = mysql_fetch_array($sth)) {
        $rows['data'][] = $r['U_Count'];
    }
}

但它不起作用。

任何人都可以帮忙吗?谢谢

4

2 回答 2

1

您可以遍历数据并将其存储为数组。

$rows = array();
while($r = mysql_fetch_array($sth)) {
   $rows[] = array('name'=>$r['Datas'],'data'=>array($r['U_Count']));
}
于 2013-10-16T12:24:12.600 回答
0

使用mysql_fetch_object()代替mysql_fetch_array()

while($r = mysql_fetch_object($result)) {
    ...
于 2013-10-16T12:18:58.750 回答