Thanks for helping!
Now it works:
<?php
$con = mysql_connect("localhost","Benutzer","Passwort");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("beta", $con);
$query = "SELECT COUNT(*) FROM Tabelle";
$result = mysql_query($query) or die(mysql_error());
list($row) = mysql_fetch_array($result);
$query2 = "SELECT SUM(Spalte) FROM Tabelle";
$result2 = mysql_query($query2) or die(mysql_error());
list($row2) = mysql_fetch_array($result2);
?>
<!doctype html>
<html lang="de">
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="2;url=diagramm.php">
<title>Titel</title>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['JaoderNein', 'Anzahl'],
['Ja', <?php echo $row2; ?>],
['Nein', <?php echo $row - $row2; ?>]
]);
var options = {
title: 'Diagramm',
is3D: true
};
var chart = new
google.visualization.PieChart(document.getElementById('piechart_3d'));
chart.draw(data, options);
}
</script>
</head>
<body>
<p> </p>
<div id="piechart_3d" style="width: 900px; height: 500px;"></div>
</body>
</html>
Am Donnerstag, 16. Januar 2014 16:45:05 UTC+1 schrieb asgallant:
>
> You are not using mysql_query correctly. You need to fetch results from
> the query and output the results to your DataTable.
>
> $row = mysql_fetch_assoc($yes);
> $countYes = $row['YesOrNo'];
> $row = mysql_fetch_assoc($no);
> $countNo = $row['YesOrNo'];
> //...
> var data = google.visualization.arrayToDataTable([
> ['YesOrNo', 'Mount'],
> ['Yes', <?php echo $countYes; ?>],
> ['No', <?php echo $countNo; ?>]
> ]);
>
> Also, if you are fetching data from a single table, you don't need to use
> two separate queries, you can combine them into one (the structure of the
> query depends on how your table is laid out, I can help you figure it out
> if you want).
>
> On Thursday, January 16, 2014 9:37:14 AM UTC-5, r1chynet wrote:
>>
>> Hi,
>>
>> how can I connect the Google Pie Chart with a MySQL database?
>>
>> My following code doesn't work :-(
>>
>> <?php
>> $con=mysqli_connect("localhost","User","Password,"testdb");
>> if (mysqli_connect_errno())
>> {
>> echo "Failed to connect to MySQL: " . mysqli_connect_error();
>> }
>> $sql = 'SELECT COUNT(*)-SUM(testcolumn) AS 'YesOrNo' FROM testtable';
>> $yes = mysql_query ( $sql );
>> $sql = 'SELECT COUNT(*)-SUM(testcolumn) AS 'YesOrNo' FROM testtable';
>> $no = mysql_query ( $sql );
>> ?>
>> <html>
>> <head>
>> <script type="text/javascript" src="https://www.google.com/jsapi
>> "></script>
>> <script type="text/javascript">
>> google.load("visualization", "1", {packages:["corechart"]});
>> google.setOnLoadCallback(drawChart);
>> function drawChart() {
>> var data = google.visualization.arrayToDataTable([
>> ['YesOrNo', 'Mount'],
>> ['Yes', $yes],
>> ['No', $no]
>> ]);
>>
>> var options = {
>> title: 'Test Pie Chart',
>> is3D: true,
>> };
>>
>> var chart = new
>> google.visualization.PieChart(document.getElementById('piechart_3d'));
>> chart.draw(data, options);
>> }
>> </script>
>> </head>
>> <body>
>> <div id="piechart_3d" style="width: 900px; height: 500px;"></div>
>> </body>
>> </html>
>>
>> Thanks for helping!
>>
>
--
You received this message because you are subscribed to the Google Groups
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.