title: Google Pie Charts and PHP
body: <p>I would like to have a Google pie chart on my website. The pie chart would be filled with data from the database. I was loooking at some examples at <a href="https://developers.google.com/chart/interactive/docs/php_example" rel="nofollow">https://developers.google.com/chart/interactive/docs/php_example</a>, but I'm lost when it comes to the JSON format.</p>
  
  <p>Here are some examples:</p>
  
  <pre><code> &lt;html&gt;
     &lt;head&gt;
  &lt;!--Load the AJAX API--&gt;
  &lt;script type="text/javascript" src="https://www.google.com/jsapi"&gt;&lt;/script&gt;
  &lt;script type="text/javascript" src="jquery-1.6.2.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
  
  // 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 jsonData = $.ajax({
        url: "getData.php",
        dataType:"json",
        async: false
        }).responseText;
  
    // 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});
  }
  
  &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
    &lt;!--Div that will hold the pie chart--&gt;
    &lt;div id="chart_div"&gt;&lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And here is the snippet where I get lost (getData.php):</p>
  
  <pre><code> &lt;?php 
  
  // This is just an example of reading server side data and sending it to the client.
  // It reads a json formatted text file and outputs it.
  
   $string = file_get_contents("sampleData.json");
   echo $string;
  
  // Instead you can query your database and parse into JSON etc etc
  
   ?&gt;
  </code></pre>
  
  <p>I have data stored in a database and not in JSON format. How do I work with the JSON format using MySQL database queries? If you have some examples or demos, I would appreciate it.</p>