I
I
Ivan Fotiev2015-07-07 01:41:43
Google
Ivan Fotiev, 2015-07-07 01:41:43

How to display data from mysql to google charts?

Good day. There is a database with a table (stat), this table has three columns (id, month, kolvo). Months are listed in the "month" column (January-December), and numbers are in "kolvo". You need to draw a chart using Google Charts.
When using static data, everything works, but when data is taken from the table, the screen is empty. If you open the console in the js section, the error is "Invalid JSON ......".
Contents of the script.js file

// Load the Visualization API and the piechart package.
      google.load('visualization', '1.0', {'packages':['corechart']});

      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);
     
      // Callback that creates and populates a data table,
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

        var jsonData = $.ajax({
          url: "data.php",
          dataType:"json",
          async: false
          }).responseText;

        // Create the data table.
        var data = new google.visualization.DataTable(jsonData);
       

        // Set chart options
        var options = {'title':'Численность рейсов',
                       'width':400,
                       'height':400,};

        // Instantiate and draw our chart, passing in some options.
      
        var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }

Contents of the data.php file
define("HOST", "localhost");
  define("USER", "root");
  define("PASSWORD", "");
  define("DB_NAME", "test");

  $db_connect = mysql_connect(HOST,USER,PASSWORD,TRUE);
  if(!$db_connect){
    die('Ошибка подключени'.mysql_error());
  }
  
  mysql_select_db(DB_NAME, $db_connect);
  echo 'Подключение прошло успешно';
  // mysql_set_charset($db,"utf8");

  function getDataString() {
    global $db_connect;
  $query=mysql_query("SELECT month, kolvo FROM stat") or die(mysql_error());
 
  $data= '{"cols":[';
  $data.='{"id":"","label":"Месяц","type":"string"},';
  $data.='{"id":"","label":"Количество","type":"number"}';
  $data.='],"rows":[';
        while($row=mysql_fetch_assoc($query)){
          $data.='{"c":[{"v":"'.$row['month']. '"},{"v":'.$row['kolvo'].'}]},';
          }       
  $data=rtrim($data,',');
  $data.=']}';
  return $data;
}
echo getDataString();

I think the error is in the loop, or rather in the output from the loop.
Error content
Error: Invalid JSON string: Connection successful{"cols":[{"id":"","label":"Month","type":"string"},{"id":"","label ":"Number","type":"number"}],"rows":[{"c":[{"v":"January"},{"v":0}]},{"c ":[{"v":"February"},{"v":0}]},{"c":[{"v":"March"},{"v":0}]},{ "c":[{"v":"April"},{"v":2}]},{"c":[{"v":"May"},{"v":0}]} ,{"c":[{"v":"June"},{"v":5}]},{"c":[{"v":"June"},{"v":5} ]},{"c":[{"v":"August"},{"v":0}]},{"c":[{"v":"September"},{"v":0}]},{"c":[{"v":"October"},{"v ":0}]},{"c":[{"v":"November"},{"v":0}]},{"c":[{"v":"December"},{ "v":0}]}]}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Valery Ryaboshapko, 2015-07-07
@ifotev

Remove this line
echo 'Подключение прошло успешно';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question