2016-02-06 63 views
0

我有一個代碼,它運行查詢並將其顯示爲頁面上的表格,並將查詢轉換爲json變量之後。不幸的是,轉換爲json變量沒有得到填充,當我打印json變量時,我只接收沒有數據的列名。在MySQL中將MySQL查詢轉換爲json

這是我的代碼:

<?php 
if (isset($_GET['variable'])) { 
    $_SESSION['variable'] = $_GET['variable']; 
    $results = mysqli_query($mysqli,"select q1.variable, t3.label, q1.numvalue, description, num_cases from (select variable, numvalue, count(variable) as num_cases from nhws.num_all_{$_SESSION['country']} where variable = '{$_SESSION['variable']}' group by variable, numvalue) q1 inner join (select * from nhws.luvalues where source = '{$_SESSION['country']}' and variable = '{$_SESSION['variable']}') t2 on q1.numvalue=t2.numvalue inner join (select * from nhws.luvariables where source = '{$_SESSION['country']}' and variable = '{$_SESSION['variable']}') t3 on q1.variable=t3.variable;"); 
    echo "<h5>Counts</h5>"; 
    if ($results->num_rows > 0) { 
     echo "<table><tr><th>Variable</th><th>label</th><th>Numvalue</th><th>Description</th><th>Num Cases</th></tr>"; 
     // output data of each row 
     while($row = $results->fetch_assoc()) { 
       echo "<tr><td>" . $row["variable"]. "</td><td>" . $row["label"]. "</td><td>" . $row["numvalue"]. "</td><td>" . $row["description"]. "</td><td>" . $row["num_cases"]. "</td></tr>"; 
     } 
     echo "</table>"; 
    } else {echo "0 results";} 

    $rows = array(); 
    //flag is not needed 
    $flag = true; 
    $table = array(); 
    $table['cols'] = array(

    // Labels for your chart, these represent the column titles 
    // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title 
    array('label' => 'variable', 'type' => 'string'), 
    array('label' => 'num_cases', 'type' => 'number') 

    ); 

    $rows = array(); 
    while($r = $results->fetch_assoc()) { 
     $temp = array(); 
     // the following line will be used to slice the Pie chart 
     $temp[] = array('v' => (string) $r["variable"]); 

     // Values of each slice 
     $temp[] = array('v' => (int) $r["num_cases"]); 
     $rows[] = array('c' => $temp); 
    } 

    $table['rows'] = $rows; 
    $jsonTable = json_encode($table); 
    echo $jsonTable; 
} 
?> 

正如你可以看到JSON變量只存儲兩列開出5列,該查詢返回的。 json變量需要存儲的列是「variable」和「num_cases」。

任何建議爲什麼json變量不會使用此代碼填充?

謝謝!

+0

您可能需要做一個http://dev.mysql.com/doc/apis-php/en/apis-php的第一個結果-mysqli-result.data-seek.html將指針重置爲返回結果的開頭。 – jeff

+0

同時檢查'echo json_last_error_msg();'是否打印任何有趣的東西。將它放在'json_encode'語句之後。 – trincot

+0

@jeff你是對的!我在while循環之前添加了這行'mysqli_data_seek($ results,0);'並且它工作正常。隨意寫它作爲答案,我會接受它。 – user3882752

回答

0

您可以在創建表格行的同一循環中爲JSON創建數組,然後進行編碼並稍後傳遞。

$dataForJson = []; 
while($row = $results->fetch_assoc()) { 
     echo "<tr><td>" . $row["variable"]. "</td><td>" . $row["label"]. "</td><td>" . $row["numvalue"]. "</td><td>" . $row["description"]. "</td><td>" . $row["num_cases"]. "</td></tr>"; 
     $dataForJson[] = $row; 
} 
echo "</table>"; 

echo json_encode($dataForJson); 
0

這個循環在這裏通過所有結果循環,每次推進指針:while($row = $results->fetch_assoc()) {,由它結束的時候,指針是在搜索結果的末尾,當您嘗試循環再次while($r = $results->fetch_assoc()) {指針仍然處於結果的最後。

在你開始第二循環,指針復位到使用mysqli_data_seek($results, 0)