我返回了一个 php 代码,用于连接我的 MYSQL 数据库并从 DB 获取数据。此数据将在谷歌图表中用于生成图表。但我的问题是我收到错误“无法加载资源文件:///C:/wamp/www/jquery-1.6.2.min.js”和“未捕获的错误:无效的 JSON 字符串:” 我知道他们有很多可用的东西我仍然无法理解我的问题。意思是如何纠正它。
我的数据库就像
id Q1-ans q2-ans
21 50 40
23 40 60
我的代码如下
<html>
<head>
<title></title>
</head>
<?php
$con=mysql_connect("localhost","root","") or die("Failed to connect with database!!!!");
mysql_select_db("mobiledb", $con);
// The Chart table contains two fields: weekly_task and percentage
// This example will display a pie chart. If you need other charts such as a Bar chart, you will need to modify the code a little to make it work with bar chart and other charts
$sth = mysql_query("SELECT `id`, `Q1`, `Q2` FROM `table2` WHERE `id`=8710058770");
$rows = array();
//flag is not needed
$flag = true;
$table = array();
$table['cols'] = array(
// Labels for your chart, these represent the column titles
// Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
array('label' => 'id', 'type' => 'number'),
array('label' => 'Q1', 'type' => 'number'),
array('label' => 'Q2', 'type' => 'number')
);
$rows = array();
while($r = mysql_fetch_assoc($sth)) {
$temp = array();
// the following line will be used to slice the Pie chart
$temp[] = array('v' => (int) $r['id']);
// Values of each slice
$temp[] = array('v' => (int) $r['Q1']);
$temp[] = array('v' => (int) $r['Q2']);
$rows[] = array('c' => $temp);
}
$table['rows'] = $rows;
$jsonTable = json_encode($table);
echo $jsonTable;
?>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="jquery-1.6.2.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: 'My values',
is3D: 'true',
width: 800,
height: 600
};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, {width: 400, height: 240});
}
</script>
<body>
<!--Div that will hold the chart-->
<div id="chart_div" ></div>
</body>
</html>