将Google (饼图)图表与MYSQL数据库集成在一起。它试图从名为'mxp937_AddStakeholder‘的数据库中检索数据,特别是列名为'ProjectRoles’的数据库,其中包含字符串数据,例如,经理、全职利益相关者、软件开发人员……GUI设计器。最终,这些数据需要显示在饼形图中,并显示信息例如,5%的用户是软件开发人员等。
我得到的错误是: Warning: 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>
发布于 2013-03-16 05:28:03
这应该是您的数据库的名称("mxp937_AddStakeholder")
mysql_select_db("mxp937_AddStakeholder", $con);这应该是从中选择结果的表的名称
$sth = mysql_query("SELECT * FROM table_name");发布于 2013-03-16 05:24:43
您是否尝试过回显从查询执行中得到的错误?因为如果你的查询没有成功运行(例如,它有一些语法问题),那么你就不会有结果可操作。试一试
or die(mysql_error());在运行查询之后。
另外,你应该停止使用mysql_*函数,开始使用mysqli_*函数,或者你可以使用PDO。mysql_*函数正在开发过程中。
https://stackoverflow.com/questions/15442214
复制相似问题