【发布时间】:2016-12-07 06:48:46
【问题描述】:
我想在从数据库中获取表格的帮助下创建谷歌饼图......但直到现在我没有得到输出......我已经搜索了几个网站......但我没有澄清任何事情......请检查我的编码并告诉
<?php
mysql_connect('localhost','root','');
mysql_select_db('chart');
$sqlquery1="select * from pie_chart";
$sqlresult1=mysql_query($sqlquery1);
$rows=array();
while($r=mysql_fetch_assoc($sqlresult1))
{
$rows[]=$r;
}
$data= json_encode($rows);
?>
<html>
<head>
<!--Load the AJAX API -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></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(){
var data = new google.visualization.DataTable();
data.addColumn("string", "Quarter");
data.addColumn("number", "Number");
data.addRows(<?php echo $data ?>);
]);
//Create our data table out of JSON data loaded from server
var data=new google.visualization.DataTable(jsonData);
//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>
</head>
<body>
<!--Div that will hold the pie chart -->
<div id="chart_div"></div>
</body>
</html>
【问题讨论】:
-
您收到的错误是什么?附带说明一下,当您希望通过 PHP 标记在 Javascript 中获取数据时,您需要使用 ECHO。因此,在您的“data.addRows();”将其更改为“data.addRows();”因为现在它在那里没有做任何事情。
-
我会改变它.. 但它会显示警告:mysql_fetch_assoc() 期望参数 1 是资源,布尔值在第 12 行的 C:\xampp\htdocs\programs\chart2.php 中给出跨度>
-
您应该远离 PHP 中的 mysql 驱动程序,因为它已被删除并且在 PhP 7.0 中将不可用,检查“php.net/manual/en/mysqli-result.fetch-assoc.php”并尝试根据它更改您的代码。如果 mysql_query 失败,它将返回 boolean (false),因此您的查询可能失败并且没有返回任何结果,因此您会收到警告。
标签: javascript php html mysql