繁体   English   中英

在PHP中将MySQL查询转换为json

[英]Converting MySQL query to json in PHP

我有一个运行查询的代码,并将其显示为页面上的表格,然后将查询转换为json变量。 不幸的是,没有填充到json变量的转换,当我打印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变量需要存储的列是“变量”和“ num_cases”。

有什么建议为什么不使用此代码填充json变量?

谢谢!

您可以在构建表行的同一循环中为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);

此循环在此处循环遍历所有结果,每次都前进指针: while($row = $results->fetch_assoc()) { ,结束时,指针位于结果的结尾,当您尝试再用while($r = $results->fetch_assoc()) {指针仍然在结果的末尾。

在开始第二个循环之前,请使用mysqli_data_seek($results, 0)将指针重置为第一个结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM