0

I want to load data from my MySQL server with php code and then use it to draw chart with google charts. the problem is that i can't use the data that quered from my mysql in the java-script code.

PHP code:

  $connection = mysql_connect('127.0.0.1','root','123456');
  mysql_select_db('db_statmarket',$connection);
  $result2 = mysql_query('select sum(`How much read from customer`) as Leads, Date from monitor group by Date;',$connection) or die('cannot show tables');

Here in the javascript code where we can see the var data = ... i want that he will be the table that i quered from my database.

HTML & JS code:

<script type="text/javascript">
  google.setOnLoadCallback(drawChart);

  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses'],
      ['2004',  1000,      400],
      ['2005',  1170,      460],
      ['2006',  660,       1120],
      ['2007',  1030,      540]
    ]);

    var options = {
      title: 'Company Performance',
      curveType: 'function',
      legend: { position: 'bottom' }
    };

    var chart = new google.visualization.LineChart(document.getElementById('curve_chart'));

    chart.draw(data, options);
  }
</script>

The output need to be seen like these: enter image description here

3
  • Either pass as javascript variable in page or use ajax. Not hard to do a web search for using either approach Commented Mar 2, 2015 at 13:23
  • @charlietfl hey charlie, how i pass $result2 as a variable? Commented Mar 2, 2015 at 13:24
  • $result2 cannot be use in javascript. It is raw SQL results, you must put them in an array using mysql_fetch_assoc() (see my answer) Commented Mar 2, 2015 at 14:08

1 Answer 1

3

Something like that:

<?php
    $connection = mysql_connect('127.0.0.1','root','123456');
    mysql_select_db('db_statmarket',$connection);
    $result2 = mysql_query('select sum(`How much read from customer`) as Leads, Date from monitor group by Date;',$connection) or die('cannot show tables');

    $json = array();
    while($row = mysql_fetch_assoc($result2)) {
        $json[] = $row;
    }
?>
<script type="text/javascript">
    var data = <?php echo json_encode($json); ?>;
    // chart code here
</script>

If your javascript is in a .js file, you can:

  1. Make your data variable "global" (though it would be better to wrap it in a global object, in order to not pollute global namespace)
  2. Use Ajax (in this case the php code would be identical, but in a separate file returning only JSON data)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but this code doesn't work. It doesn't fir the 'var data' format table that written in HTML code.
You must adapt it to your needs. I cannot provide you with a complete copy/paste code without knowing more (and I don't actually think this is gonna help you). Just check the value of "data" and modify your sql request or javascript to fit your needs (first problem I see, you select 2 columns but want an output with 3 columns). You will also have to manually add to "data" the first entry (['Year', 'Sales', 'Expenses']). Feel free to let me knowif you need more help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.