2013-07-24 227 views
0

我有回到它是用來連接我的MySQL數據庫,並從數據庫中獲取數據的PHP代碼谷歌圖表中的數據。這些數據將用於谷歌圖表生成圖表。 但我的問題是我得到一個錯誤「無法加載資源文件:/// C:/wamp/www/jquery-1.6.2.min.js」和「未捕獲的錯誤:無效的JSON字符串:」 我知道他們有很多可用的東西,但我無法理解我的問題。意味着如何糾正它。檢索從數據庫中使用PHP

我的數據庫是像

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> 

回答

0

你必須改變:

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

我懷疑,這個問題是該行的報價:

data.addRows('<?php $data ?>'); 

,因爲他們正在採取什麼應該是一個數組的數組,並把它變成一個字符串。嘗試刪除引號:

data.addRows(<?php $data ?>); 

並再次運行腳本。如果不解決這個問題,無論是張貼鏈接到網頁或生成的JavaScript(在瀏覽器中打開網頁,源代碼從那裏複製),所以我可以去看看。