How to use <?php echo $data; ?> in order to populate a Google chart
<p>I am currently working on a project at university which involves sampling values
(CO2, temperature, humidity ...) to a SQLite3 database with a Raspberry Pi and using a webinterface to show the data. 
I'm fairly new to php/html, but I have no problems understanding what the code does.</p>

<p>I already found a <a href="https://raspberry.tips/hausautomatisierung/temperatur-diagramm-erstellen-mit-google-charts/" rel="nofollow">german tutorial</a> describing how to create a php/html script which imports the data from the SQLite database and uses Google Graphs in order to display it. I used the provided code as the base for my script.</p>

<p>This is what I have so far:</p>

<pre><code>&lt;?php

require_once("config.php");
require_once("functions.php");
//------------------------ PHP Settings ----------------------------------------
ini_set('track_errors', 1);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set("memory_limit","64M");
ini_set("max_execution_time","30");
@ob_implicit_flush(true);
@ob_end_flush();
$_SELF=$_SERVER['PHP_SELF'];


$SQL="SELECT CO2, Temperature FROM value";

$db = db_con($DBfile);
$q = db_query($SQL);

 $data = "var data = new google.visualization.DataTable();&lt;br&gt;"
 ."data.addColumn('number', 'CO2');&lt;br&gt;"
 ."data.addColumn('number', 'Temperature');&lt;br&gt;&lt;br&gt;"
 ."data.addRows([&lt;br&gt;";

 while ($res = $q-&gt;fetch(PDO::FETCH_ASSOC)) {

        $temp = (int)$res['Temperature'];
        $co2 = (int)$res['CO2'];
        $data = $data."  [".$temp.", ".$co2."],&lt;br&gt;";
        }
        $data = $data."]);&lt;br&gt;";

//Print data to check if data from database is loaded
echo $data;

?&gt;

 &lt;html&gt;
  &lt;head&gt;
    &lt;script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"&gt;&lt;/sc$
    &lt;script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"&gt;&lt;/script&gt;
    &lt;script type="text/javascript"&gt;
      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);

      function drawChart() {
        &lt;?php echo $data; ?&gt;

        var options = {
          title: 'Air Quality',
          curveType: 'function',
          legend: { position: 'bottom' }
        };

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

        chart.draw(data, options);
      }
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id="temp_curve_chart" style="width: 900px; height: 500px"&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>

<p>Now the problem is, that the graph does not show up.</p>

<p>If I add <code>echo $data;</code>the content of the variable shows up in the browser, which means that the import part is working, but I just can't get it to display the graph.</p>

<pre><code>var data = new google.visualization.DataTable();
data.addColumn('number', 'CO2');
data.addColumn('number', 'Temperature');

data.addRows([
[23, 500],
[23, 500],
[25, 600],
]);
</code></pre>

<p>I suspect it has something to do with the following line of code or with the way the $data variable is set up:</p>

<pre><code>function drawChart() {
        &lt;?php echo $data; ?&gt;
</code></pre>

<p>If I replace it with code from the example <a href="https://developers.google.com/chart/interactive/docs/gallery/linechart" rel="nofollow" title="from google">provided by Google</a>, it shows the graph just fine.</p>

<pre><code>  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' }
    };
</code></pre>

<p>I already spent hours trying to get it work, but I just can't get it right.</p>

<p>Any help/tips would be greatly appreciated!</p>

<p>Thanks in advance!</p>