Home >Database >Mysql Tutorial >How to Create a Google Chart from MySQL Data using PHP and JSON?

How to Create a Google Chart from MySQL Data using PHP and JSON?

Linda Hamilton
Linda HamiltonOriginal
2024-12-11 07:01:11119browse

How to Create a Google Chart from MySQL Data using PHP and JSON?

PHP MySQL Google Chart JSON: A Comprehensive Example

This guide provides a comprehensive example on how to generate a Google Chart using data from a MySQL table. We'll demonstrate the integration of PHP, MySQL, and the Google Chart API to create a visual representation of the data.

Requirements:

  • PHP
  • Apache
  • MySQL

Installation:

  1. Create a MySQL database named "chart."
  2. Create a table named "googlechart" with two columns: "weekly_task" and "percentage."
  3. Insert sample data into the table, with "percentage" representing numerical values.

PHP-MySQL-JSON-Google Chart Example:

<?php
// Connect to the MySQL database
$con = mysql_connect("localhost", "Username", "Password") or die("Failed to connect with database!!!!");
mysql_select_db("Database Name", $con);

// Query the database for weekly tasks and percentages
$sth = mysql_query("SELECT * FROM chart");

// Prepare the data for the Google Chart
$table['cols'] = array(
    array('label' => 'Weekly Task', 'type' => 'string'),
    array('label' => 'Percentage', 'type' => 'number')
);

$rows = array();
while ($r = mysql_fetch_assoc($sth)) {
    $temp = array();
    $temp[] = array('v' => (string)$r['Weekly_task']);
    $temp[] = array('v' => (int)$r['percentage']);
    $rows[] = array('c' => $temp);
}

$table['rows'] = $rows;
$jsonTable = json_encode($table);

// Load the Google Chart API and create a pie chart
?>

<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 = new google.visualization.DataTable(<?=$jsonTable?>);
        var options = {
          title: 'My Weekly Plan',
          is3D: 'true',
          width: 800,
          height: 600
        };
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>

  <body>
    <div>

Error Handling:

If you encounter a "syntax error" related to short tags, use the following instead:

<?php echo $jsonTable; ?>

This ensures that PHP tags are properly closed and interpreted by your environment.

The above is the detailed content of How to Create a Google Chart from MySQL Data using PHP and JSON?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn