0

I already have checked similar answer. But i have different issue here!

TO create googlce char I am creating json string by fetching data from table. Table has only two rows. Here is how the json string is constructed:

 $result = $mysqli->query('SELECT * FROM view_name');

   $p= $mysqli->query('SELECT Index_val FROM view_name where ind_type=pcount');
 //echo "$p";

  $rows = array();
  $table = array();
  $table['cols'] = array(
    array('label' => 'pcount', 'type' => 'string'),
    array('label' => 'ncount', 'type' => 'number')
);
    /* Extract the information from $result */
    foreach($result as $r) {
      $temp = array();
      $temp[] = array('v' => (string) $r['ind_type']); 
      $temp[] = array('v' => (int) $r['Index_val']); 
      $rows[] = array('c' => $temp);
    }

$table['rows'] = $rows;

Here only two values I need to fetch from database pcount and ncount. I dont want to use above procedeure.

What I want is to fetch the values of pcount and ncount from table manually, store in variable and construct the json string.

Like this : $p= $mysqli->query('SELECT Index_val FROM view_name where ind_type=pcount'); // Please correct if wrong same way get get ncount.

Now how can I create above json string from this? Resulting string will be like this:

{
    "cols":[
        {"label":"pcount","type":"string"},
        {"label":"ncount","type":"number"}],
    "rows":[
        {"c":
            [
            {"v":"pcount"},
            {"v":179}  // 179 is pcount
            ]
        },
        {"c":
            [
            {"v":"ncount"},
            {"v":285} //285 is ncount
            ]
        }
        ]
}
user123
  • 5,269
  • 16
  • 73
  • 121
  • 1
    possible duplicate of [JSON encode MySQL results](http://stackoverflow.com/questions/383631/json-encode-mysql-results) – Nebril Sep 30 '13 at 15:06
  • @ghaxx: I already have tried that method. But my above case I need some customization not in the way you gave link. I appreciate if you can show effort! – user123 Sep 30 '13 at 15:52
  • First of all, you need to understand how to work with mysqli. You don;t just query and the data magicially populates into an array. Query just returnsa result set handle, which you must iterate over to get the data from the database. – Mike Brant Sep 30 '13 at 15:57

0 Answers0