2016-11-08 74 views
0

我在這裏有一點問題,我想用PHP生成一些數據從MySQL特定的JSON格式,這是我的PHP代碼生成JSON從MySQL使用PHP

<?php 


/* 
    Get data from the mysql database and return it in json format 
*/ 


//setup global vars 
$debug = $_GET['debug']; 
$format = $_GET['format']; 

if($format=='json'){ 
    header("Content-type: text/json"); 
} 


$db = new mysqli('localhost', root, 'kudanil123', 'PT100', 3306); 

if (mysqli_connect_error()) { 
    die('Connect Error (' . mysqli_connect_errno() . ') ' 
      . mysqli_connect_error()); 
} 

    if ($debug == 1) {echo 'Success... ' . $db->host_info . "\n";} 

// get data 
$sql = "select meas_date,ai0_hist_value"; 
$sql .= " from ai0_hist"; 
$sql .= " where board_temp_hist_value > 30"; //filter out bad data 
$sql .= " group by 1"; 
$sql .= " order by meas_date desc"; //highcarts requires you order dates in asc order 
$sql .= " limit 5;"; 


if ($result = $db->query($sql)) { 

    if ($debug == 1) {echo "fetched data! <br/><br/>";} 


    while($row = $result->fetch_array()){ 
     $rows[] = $row; 
    } 

    foreach($rows as $row){ 

     $text[] = (float)$row['ai0_hist_value']; 
     $date[] = strtotime($row['meas_date'])*1000; 

    } 
      } 
     //$data[0] = $names; 
     $data1 = $date; 
     $data = $text; 
     $data2 = array($data1, $data); 
     //$data[2] = $text; 
     echo (json_encode($data2)); 
     // echo(json_encode($names)); 
     $result->close(); 

} else { 
    echo "Error: " . $sql . "<br>" . $db->error; 
} 

$db->close(); 

?> 

有了這個代碼,結果是

[ 
    [1478616679000, 1478616677000, 1478616675000, 1478616673000, 1478616671000], 
    [28.4126, 28.5361, 28.4126, 28.4126, 28.2891] 
] 

是的,這是有效的JSON,但是,我想用這個JSON在highcharts.com圖表,所以我需要的JSON格式類似這樣的

[ 
    [1257811200000, 29.00], 
    [1257897600000, 29.04], 
    [1257984000000, 28.86], 
    [1258070400000, 29.21], 
    [1258329600000, 29.52], 
    [1258416000000, 29.57], 
    [1258502400000, 29.42], 
    [1258588800000, 28.64], 
    [1258675200000, 28.56], 
    [1258934400000, 29.41], 
    [1259020800000, 29.21], 
    [1259107200000, 29.17], 
    [1259280000000, 28.66], 
    [1259539200000, 28.56] 
] 

欣然如果有人能幫助我,我又立刻陷入了幾天試圖解決這一問題

回答

0

可以構建格式化的一系列數據開始與象下面這樣:

<?php 


/* 
    Get data from the mysql database and return it in json format 
*/ 


//setup global vars 
$debug = $_GET['debug']; 
$format = $_GET['format']; 

if($format=='json'){ 
    header("Content-type: text/json"); 
} 


$db = new mysqli('localhost', root, 'kudanil123', 'PT100', 3306); 

if (mysqli_connect_error()) { 
    die('Connect Error (' . mysqli_connect_errno() . ') ' 
      . mysqli_connect_error()); 
} 

    if ($debug == 1) {echo 'Success... ' . $db->host_info . "\n";} 

// get data 
$sql = "select meas_date,ai0_hist_value"; 
$sql .= " from ai0_hist"; 
$sql .= " where board_temp_hist_value > 30"; //filter out bad data 
$sql .= " group by 1"; 
$sql .= " order by meas_date desc"; //highcarts requires you order dates in asc order 
$sql .= " limit 5;"; 


if ($result = $db->query($sql)) { 

    if ($debug == 1) {echo "fetched data! <br/><br/>";} 


    while($row = $result->fetch_array()){ 
     $rows[] = $row; 
    } 

    foreach($rows as $row){ 
     $seriesData[] = [ strtotime($row['meas_date'])*1000, (float)$row['ai0_hist_value'] ]; 

    } 


     echo (json_encode($seriesData)); 

     $result->close(); 

} else { 
    echo "Error: " . $sql . "<br>" . $db->error; 
} 

$db->close(); 
1

,這將產生你想要的數組,就沒有必要做的數據全部擺弄從數據庫

// get data 
$sql = "select meas_date,ai0_hist_value"; 
$sql .= " from ai0_hist"; 
$sql .= " where board_temp_hist_value > 30"; //filter out bad data 
$sql .= " group by 1"; 
$sql .= " order by meas_date desc"; //highcarts requires you order dates in asc order 
$sql .= " limit 5;"; 


$rows = array(); 
if ($result = $db->query($sql)) { 

    while($row = $result->fetch_array()){ 

     $rows[] = array(strtotime($row['meas_date'])*1000, 
         $row['ai0_hist_value'] 
         ); 
    } 
} 
echo json_encode($rows); 

現在您將需要將文本轉換爲在JavaScript中浮動。這是因爲JSON作爲文本而不是任何其他數據類型傳遞,因此必須在接收javascript的情況下進行轉換。

1

如果你想這樣的代碼,你必須修復代碼:

while($row = $result->fetch_array()){ 
    $rows[] = $row; 
} 

foreach($rows as $row){ 

    $text[] = (float)$row['ai0_hist_value']; 
    $date[] = strtotime($row['meas_date'])*1000; 

} 

//$data[0] = $names; 
    $data1 = $date; 
    $data = $text; 
    $data2 = array($data1, $data); 
    //$data[2] = $text; 
    echo (json_encode($data2)); 

必須是這樣的:

while($row = $result->fetch_array()){ 
    $rows[] = array(
      (float)$row['ai0_hist_value'], 
      strtotime($row['meas_date'])*1000); 
} 

echo (json_encode($rows)); 

您在$ data2中保存了一個包含兩個數組(文本和數據)的數組。您必須爲每一對「文字」和「數據」保存一行。