【问题标题】:How to create Google Chart friendly JSON arrays from MySQL?如何从 MySQL 创建 Google Chart 友好的 JSON 数组?
【发布时间】:2013-08-07 20:32:22
【问题描述】:

我想我已经接近完成通过 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;

?>

【问题讨论】:

  • 我从您的代码中假设您正在使用 PDO 进行数据库 I/O,这是正确的,还是您使用的是另一个看起来类似于 PDO 的系统?

标签: php mysql ajax json google-visualization


【解决方案1】:

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

<?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}]}]}

【讨论】:

    【解决方案2】:

    尝试替换这一行:

    $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();
    

    【讨论】:

    • 您建议的更新输出:{"cols":[{"id":"","label":"projid","type":"string"},{"id":"","label":"hours","type":"number"}],"rows":[]} 什么会导致查询数据无法传递到数组?
    • 这可能是由于拼写错误 - 我错过了应该在 $results-&gt;fetch_assoc() 前面的“$”。用修复编辑了我上面的答案。
    • 我确实注意到在测试您的解决方案之前缺少“$”。我更新了问题的代码以纳入您的建议。是否有任何进一步的错误导致输出不包含 SQL 数据?您会建议将 MySQLi 查询传递给 JSON 的不同方法吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2013-08-23
    • 2022-09-22
    • 2013-04-28
    相关资源
    最近更新 更多