SQL数据库中的HTML表格显示

时间:2013-03-15 21:15:45

标签: mysql charts google-visualization

努力将Google(饼图)图表与MYSQL数据库集成。它正在尝试从名为“mxp937_AddStakeholder”的数据库中检索数据,特别是列名称为“ProjectRoles”,其中包含字符串数据,例如Manager,Full Time Stakeholder,Software Developer .... GUI Designer。最终,这些数据需要显示在饼图中并显示信息,例如,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>

2 个答案:

答案 0 :(得分:0)

您是否尝试过回显查询执行中出现的错误?因为如果您的查询未成功运行(例如,它有一些语法问题),那么您将无法操作结果。试试

or die(mysql_error());
运行查询后

此外,您应该停止使用mysql_ *函数并开始使用mysqli_ *,或者您可以使用PDO。 mysql_ *函数正处于eed depricated的过程中。

答案 1 :(得分:0)

这应该是您的数据库的名称(“mxp937_AddStakeholder”)

mysql_select_db("mxp937_AddStakeholder", $con);

这应该是您从

中选择结果的表格的名称
$sth = mysql_query("SELECT * FROM table_name");