1

几天前,我发现了 HighStock。我想使用 SQL 数据库中的数据。通过 php 我读出了信息并以 JSON 样式回显它

这是脚本

<?php
include ("dbverbindung.php");
echo "[";
$sql = "SELECT time, amperage FROM pikkertonnode_44664399";
$erg = mysql_query($sql);
while ($data= mysql_fetch_array($erg))
{
if ($data['amperage'] == "") $data['amperage'] = 0;
echo "[".$data['time'].",".$data['amperage']."],"   ;
}
echo "]";    ?>

输出看起来是这样的:

[[1264978800000,68],[1264978805000,50],[1264978810000,61],[1264978815000,54]]

它是否正确?

4

3 回答 3

0

用这个:

<?php
include ("dbverbindung.php");
echo "[";
$sql = "SELECT time, amperage FROM pikkertonnode_44664399";
$erg = mysql_query($sql);
$temp_array=array();
while ($data= mysql_fetch_array($erg))
{
array_push(temp_array,data);
}
echo json_encode($temp_array);   // make json data
?>

两种格式都是正确的..

{                                 [
  {'1264978800000':'68'},           [1264978800000,68],
  {'1264978805000':'50'},           [1264978805000,50],
  {'1264978810000':'61'},           [1264978810000,61],
  {'1264978815000':'54'}            [1264978815000,54], 
}                                 ]
于 2013-01-07T17:11:59.017 回答
0

您应该使用json_encode. 只需按照您想要的方式制作数组,然后让 PHP 对其进行编码。

<?php
include ("dbverbindung.php");
$sql = "SELECT time, amperage FROM pikkertonnode_44664399";
$erg = mysql_query($sql);

$json = array();
while ($data= mysql_fetch_array($erg)){
    if ($data['amperage'] == ""){
        $data['amperage'] = 0;
    }

    $json[] = array($data['time'], $data['amperage']);
}

echo json_encode($json);
?>
于 2013-01-07T17:19:30.613 回答
0

行。问题是 Highchart/Highstock 不接受字符串。JSONdata 正确生成

例如 [["123","12"],["123","12"],["123","12"]]

但这对“”没有用

代码应该看起来像

while ($data= mysql_fetch_array($erg)){
if ($data['amperage'] == ""){
    $data['amperage'] = 0;
}
$x = $data['time'];
$y = $data['amperage'];

$json[] = array( (int($x), int($y) );
}

现在我有了我想要的代码.. Highstock 现在可以完美运行

[[123,12],[123,12],[123,12]]

于 2013-01-11T13:37:58.010 回答