0

我想我已经接近完成通过 JSON/AJAX 将 MySQL 数据传递到 Google Charts 的工作了。我能够以正确的格式输出 JSON 字符串,但它没有输出任何 SQL 数据。我到处寻找没有结果的解决方案。有人看到代码中缺少什么吗?

JSON 输出

{"cols":[{"id":"","label":"projid","type":"string"},{"id":"","label":"hours","type":"number"}],"rows":[{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]},{"c":[{"v":""},{"v":0}]}]}

PHP->JSON

<?php
// -----> Query MySQL and parse into JSON below. <------

// write your SQL query here (you may use parameters from $_GET or $_POST if you need them)

require_once ("Includes/session.php");
require_once ("Includes/simplecms-config.php");
require_once ("Includes/connectDB.php");

$recId = null;
$projid = null;
$hours = null;

        $recId = $_GET['id'];
        $projid = $_GET['projid'];
        $hours = $_GET['hours'];
        $query = "SELECT projid, hours FROM hours WHERE id = ?";
        $statement = $databaseConnection->prepare($query);
        $statement->bind_param('d', $recId);
        $statement->execute();
        $results = $statement->get_result();

  $rows = array();
  $table = array();
  $table['cols'] = array(
    array('id' => "",'label' => 'projid', 'type' => 'string'),
    array('id' => "",'label' => 'hours', 'type' => 'number')
);


    /* Extract the information from $result */
    while ($r = $results->fetch_assoc()) {
      $temp = array();

      // The following line will be used to slice the Pie chart
      $temp[] = array('v' => (string) $r['projid']); 

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

$table['rows'] = $rows;

// convert data into JSON format
$jsonTable = json_encode($table);
echo $jsonTable;

?>

4

2 回答 2

3

以下代码为 Google Charts 返回了正确的数组。谷歌图表 - JSON 数据

<?php 

// -----> Query MySQL and parse into JSON below. <------

require_once  ("Includes/connectDB.php");

$result = $databaseConnection->query("SELECT projid, hours FROM alloc_hours");      
    $table = array();
    $table['cols'] = array(
    array('id' => "", 'label' => 'projid', 'pattern' => "", 'type' => 'string'),
    array('id' => "", 'label' => 'hours', 'pattern' => "", 'type' => 'number')
    );
    $rows = array();
    while ($nt = $result->fetch_assoc())
    {
    $temp = array();
    $temp[] = array('v' => $nt['projid'], 'f' =>NULL);
    $temp[] = array('v' => $nt['hours'], 'f' =>NULL);
    $rows[] = array('c' => $temp);
    }
    $table['rows'] = $rows;
    $jsonTable = json_encode($table);
    echo $jsonTable;
?>

大批

{"cols":[{"id":"","label":"projid","pattern":"","type":"string"},{"id":"","label":"hours","pattern":"","type":"number"}],"rows":[{"c":[{"v":"2","f":null},{"v":"8","f":null}]},{"c":[{"v":"1","f":null},{"v":"6","f":null}]},{"c":[{"v":"3","f":null},{"v":"20","f":null}]},{"c":[{"v":"2","f":null},{"v":"10","f":null}]},{"c":[{"v":"4","f":null},{"v":"5","f":null}]},{"c":[{"v":"1","f":null},{"v":"30","f":null}]}]}
于 2013-08-08T13:49:34.217 回答
0

尝试替换此行:

$statement->store_result();

和:

$results = $statement->get_result();

并用foreach循环替换while循环:

while ($r = $results->fetch_assoc()) {
    $temp = array();

    // The following line will be used to slice the Pie chart

    $temp[] = array('v' => (string) $r['projid']);

    // Values of the each slice

    $temp[] = array('v' => (int) $r['hours']);
    $rows[] = array('c' => $temp);
}

那应该让查询返回结果。您不需要以下行:

$statement->bind_result($projid, $hours);
$statement->fetch();
于 2013-08-06T23:05:23.707 回答