2013-08-06 26 views
0

我想我已经接近完成通过JSON/AJAX将MySQL数据传递给Google Charts。我能够以正确的格式输出JSON字符串,但它不输出任何SQL数据。我到处搜索没有结果的解决方案。任何人都可以看到代码中缺少什么?如何从MySQL创建Google Chart友好的JSON数组?

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; 

>

+0

我从您的代码假设你正在使用PDO的为你的数据库I/O,是正确的,或者您使用另一个系统,类似于PDO? – asgallant

+0

我正在使用MySQLi。 – Universal

回答

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}]}]} 
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(); 
+0

您建议输出的更新:'{“cols”:[{“id”:“”,“label”:“projid”,“type”:“string”},{“id”:“”,“label” :“hours”,“type”:“number”}],“rows”:[]}'什么会导致查询数据不传递给数组? – Universal

+0

这可能是由于输入错误 - 我错过了应该在$ results-> fetch_assoc()前面的“$”。修正以上编辑我的答案。 – asgallant

+0

在测试解决方案之前,我注意到“$”缺失。我更新了问题的代码,以纳入您的建议。是否还有其他错误导致输出不包含SQL数据?你会建议将MySQLi查询传递给JSON的另一种方法吗? – Universal