努力将谷歌(饼图)图表与 MYSQL 数据库集成。它正在尝试从名为“mxp937_AddStakeholder”的数据库中检索数据,特别是列名是“ProjectRoles”,其中包含字符串数据,例如,经理、全职利益相关者、软件开发人员...... GUI 设计师。最终需要将这些数据显示为饼图并显示信息,例如,5% 的用户是软件开发人员等。
我得到的错误是:警告:mysql_fetch_assoc():提供的参数不是有效的 MySQL 结果资源。
请指教?
<?php
$con=mysql_connect("localhost","username","password") or die("Failed to connect with database!");
mysql_select_db("mxp937_AddStakeholder", $con);
$sth = mysql_query("SELECT projectroles FROM mxp937_AddStakeholder");
$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(
array('label' => 'Project Roles', 'type' => 'string'),
);
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$temp = array();
// the following line will used to slice the Pie chart
$temp[] = array('v' => (string) $r['ProjectRoles']);
//Values of the each slice
$temp[] = array('v' => (int) $r['percentage']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
//echo $jsonTable;
?>
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
// Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(<?=$jsonTable?>);
var options = {
title: 'Project Roles',
is3D: 'true',
width: 800,
height: 600
};
// Instantiate and draw our chart, passing in some options.
//do not forget to check ur div ID
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>