Connection Google Pie Chart with MySQL database

1,153 views
Skip to first unread message

r1chynet

unread,
Jan 16, 2014, 9:37:14 AM1/16/14
to google-visua...@googlegroups.com
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!

asgallant

unread,
Jan 16, 2014, 10:45:05 AM1/16/14
to google-visua...@googlegroups.com
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).

r1chynet

unread,
Jan 21, 2014, 5:36:47 AM1/21/14
to google-visua...@googlegroups.com
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>&nbsp;</p>
    <div id="piechart_3d" style="width: 900px; height: 500px;"></div>
  </body>
</html>
Reply all
Reply to author
Forward
0 new messages