Converting MySQL query to json in PHP -


i have code runs query , display table on page , right after convert query json variable. unfortunately, conversion json variable not populated , when print json variable receive column names without data.

this code:

<?php if (isset($_get['variable'])) {     $_session['variable'] = $_get['variable'];     $results = mysqli_query($mysqli,"select q1.variable, t3.label, q1.numvalue, description, num_cases (select variable, numvalue, count(variable) num_cases nhws.num_all_{$_session['country']} variable = '{$_session['variable']}' group variable, numvalue) q1 inner join (select * nhws.luvalues source = '{$_session['country']}' , variable = '{$_session['variable']}') t2 on q1.numvalue=t2.numvalue inner join (select * nhws.luvariables source = '{$_session['country']}' , variable = '{$_session['variable']}') t3 on q1.variable=t3.variable;");     echo "<h5>counts</h5>";     if ($results->num_rows > 0) {          echo "<table><tr><th>variable</th><th>label</th><th>numvalue</th><th>description</th><th>num cases</th></tr>";          // output data of each row          while($row = $results->fetch_assoc()) {               echo "<tr><td>" . $row["variable"]. "</td><td>" . $row["label"]. "</td><td>" . $row["numvalue"]. "</td><td>" . $row["description"]. "</td><td>" . $row["num_cases"]. "</td></tr>";          }          echo "</table>";     } else {echo "0 results";}       $rows = array();     //flag not needed     $flag = true;     $table = array();     $table['cols'] = array(      // labels chart, these represent column titles     // note 1 column in "string" format , 1 in "number" format pie chart required "numbers" calculating percentage , string used column title     array('label' => 'variable', 'type' => 'string'),     array('label' => 'num_cases', 'type' => 'number')      );      $rows = array();     while($r = $results->fetch_assoc()) {          $temp = array();          // following line used slice pie chart          $temp[] = array('v' => (string) $r["variable"]);            // values of each slice          $temp[] = array('v' => (int) $r["num_cases"]);          $rows[] = array('c' => $temp);     }      $table['rows'] = $rows;     $jsontable = json_encode($table);     echo $jsontable; } ?> 

as can see json variable stores 2 columns out of 5 columns query returns. columns json variable need store "variable" , "num_cases".

any suggestions why json variable doesn't populated using code?

thanks!

you can create array json in same loop build table rows, , encode , deliver later.

$dataforjson = [];   while($row = $results->fetch_assoc()) {       echo "<tr><td>" . $row["variable"]. "</td><td>" . $row["label"]. "</td><td>" . $row["numvalue"]. "</td><td>" . $row["description"]. "</td><td>" . $row["num_cases"]. "</td></tr>";       $dataforjson[] = $row; } echo "</table>";  echo json_encode($dataforjson); 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -