使用PHP从google图表数据库中检索数据

使用PHP从google图表数据库中检索数据,php,google-visualization,Php,Google Visualization,我返回了一个php代码,用于连接我的MYSQL数据库并从数据库中获取数据。这些数据将在谷歌图表中用于生成图表。 但我的问题是,我收到了一个错误,加载资源失败file:///C:/wamp/www/jquery-“1.6.2.min.js”和“未捕获错误:无效JSON字符串:” 我知道有很多东西可以利用,但我仍然无法理解我的问题。意味着如何纠正它 我的数据库就像 id Q1-ans q2-ans 21 50 40 23 40 60 我的代码如下

我返回了一个php代码,用于连接我的MYSQL数据库并从数据库中获取数据。这些数据将在谷歌图表中用于生成图表。 但我的问题是,我收到了一个错误,加载资源失败file:///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>

//加载可视化API和piechart包。
load('visualization','1',{'packages':['corechart']});
//将回调设置为在加载Google Visualization API时运行。
setOnLoadCallback(drawChart);
函数绘图图(){
//使用从服务器加载的JSON数据创建我们的数据表。
var data=new google.visualization.DataTable(“”);
变量选项={
标题:“我的价值观”,
is3D:'正确',
宽度:800,
身高:600
};
//实例化并绘制图表,传入一些选项。
var chart=new google.visualization.PieChart(document.getElementById('chart_div');
绘制图表(数据,{宽度:400,高度:240});
}
您必须更改:

var data = new google.visualization.DataTable(<?php echo $jsonTable; ?>);
var data=new google.visualization.DataTable();

我怀疑问题出在这一行的引号上:

data.addRows('<?php $data ?>');
data.addRows(“”);
当他们把应该是数组的数组转换成字符串时。尝试删除引号:

data.addRows(<?php $data ?>);
data.addRows();
然后再次运行脚本。如果这不能解决问题,可以发布一个指向页面的链接或生成的javascript(在浏览器中打开页面,从那里复制源代码),这样我就可以查看了