2013-03-15 149 views
0

努力將Google(餅圖)與MYSQL數據庫集成。它試圖從名爲'mxp937_AddStakeholder'的數據庫檢索數據,特別是列名是'ProjectRoles',其中包含字符串數據,例如Manager,Full Time Stakeholder,Software Developer .... GUI Designer。最終這些數據需要顯示在餅圖中並顯示信息,例如5%的用戶是軟件開發人員等。來自SQL數據庫的HTML表格顯示

我得到的錯誤是: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> 

回答

0

這應該是

mysql_select_db("mxp937_AddStakeholder", $con); 

這應該是你選擇的結果表從

$sth = mysql_query("SELECT * FROM table_name"); 
名稱數據庫(「mxp937_AddStakeholder」)的名稱
0

您是否嘗試過呼應你的查詢執行得到錯誤?因爲如果你的查詢沒有成功運行(例如它有一些語法問題),那麼你將不會有結果操縱。嘗試

or die(mysql_error()); 

運行查詢後。另外,你應該停止使用mysql_ *函數並開始使用mysqli_ * ones,或者你可以使用PDO。 mysql_ *函數正處於depicated過程中。