first of all sorry this is so amatuer, I'm still very much a beginner.
I'm practising interacting with databases in PHP and displaying it nicely using Google Visualization. However my chart won't display and I think it's because of how I'm passing the data into the chart, since I've used Google Charts before and the only thing I'm doing differently is using $row to put the info into the chart.
Or am I going about this the wrong way and I should be putting $row into a new array and then passing that into the chart?
Many thanks!
Here is my code:
<?php
//this script retrieves all data from the fruit table and displays it in a google chart
$page_title = "View the fruit table";
require_once ('connect.php'); //connects to mysql db
//make the query
$query = "SELECT *
FROM fruits";
$result = @mysql_query ($query); //runs the query
if ($result) { //if it ran alright, display the records
//load the JSAPI library
echo '<table align="center" cellspacing="2" cellpadding="2">
<tr><td align="left"><b>Name of fruit</b></td><td align="left"><b>Amount</b></td></tr>';
//fetch and print all the records
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
echo "<tr><td align=\"left\">$row[0]</td><td align=\"left\">$row[1]</td></tr>
";
}
//load google visualization API library, piechart library and JSAPI library
echo '<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1.0",{"packages":["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart(){
//create the data table
var data = new google.visualization.DataTable();
data.addColumn("string","Fruits");
data.addColumn("number","Amount");
data.addRows([["$row[0]","$row[1]"]]);
//set chart options
var options = {"title":"Amount of different fruits",
"width":400,
"height":300};
//instantiate and draw chart, passing in options
var options = new google.visualization.PieChart(document.getElementById("chart_div"));
chart.draw(data, options);
} //end of drawchart function
</script>';
//display chart
echo '<div id="chart_div"></div>';
echo '</table>';
mysql_free_result($result); //free up the resources
} else{ //if it did not run alright
echo '<p>The table could not be displayed due to a system error.</p><p>' . mysql_error() . '</p>';
}
mysql_close(); //close the database connection
?>