Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:57:11+00:00 2026-05-26T01:57:11+00:00

I’m trying to create a chart using the Google Visualization API, with PHP &

  • 0

I’m trying to create a chart using the Google Visualization API, with PHP & MySQL in the background.

What I’m doing is:

  • getting the data from db using PHP / SQL

    $sth = mysql_query("SELECT * FROM Chart");
    
  • creating JSON with PHP

    $rows = array();
    
    while($r = mysql_fetch_assoc($sth)) {
      $rows[] = $r;
    }
    
    $jdata = json_encode($rows);
    
  • and then feeding Google Visualization API with JSON

    var data = new google.visualization.DataTable(<?php echo $jdata ?>); 
    

Just to make sure the JSON is actually in the correct format I did:

$jdata = json_encode($rows);
print $jdata;

which returned:

[{"id":"1","quarters":"1","salary":"1250"},{"id":"2","quarters":"2","salary":"2500"},{"id":"3","quarters":"3","salary":"4526"},{"id":"4","quarters":"4","salary":"4569"}] 

So,

  • db connection is OK.
  • creating JSON from PHP array is OK.
  • JSON format is OK.

Firebug is returning an error saying:

Table has no columns.
[Break On This Error] b,Sl),[b]}function Zq(a,b){var c=a[xc]…”].”)):d(l(“Table has no columns.”))}

The question is how can I create columns from JSON data?

UPDATE:

Code used to create the graph below:

// SQL Query
$sth = mysql_query("SELECT * FROM Chart");
//$rows = array();

while($r = mysql_fetch_assoc($sth)) {
   if(!isset($google_JSON)){    
     $google_JSON = "{cols: [";    
     $column = array_keys($r);
     foreach($column as $key=>$value){
         $google_JSON_cols[]="{id: '".$key."', label: '".$value."'}";
     }    
     $google_JSON .= implode(",",$google_JSON_cols)."],rows: [";       
   }
   $google_JSON_rows[] = "{c:[{v: '".$r['id']."'}, {v: '".$r['quarters']."'}, {v: '".$r['salary']."'}]}";
}    

// you may need to change the above into a function that loops through rows, with $r['id'] etc, referring to the fields you want to inject..

$data = $google_JSON.implode(",",$google_JSON_rows)."]}";

Output HTML CODE:

        <!-- load Google AJAX API -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">  
        //load the Google Visualization API and the chart  
        google.load('visualization', '1', {'packages':['columnchart']});  

        //set callback  
        google.setOnLoadCallback (createChart);  

        //callback function  
        function createChart() {  

            //create data table object  
            var data = new google.visualization.DataTable({cols: [{id: '0', label: 'id'},{id: '1', label: 'quarters'},{id: '2', label: 'salary'}],rows: [{c:[{v: '1'}, {v: '1'}, {v: '1250'}]},{c:[{v: '2'}, {v: '2'}, {v: '2500'}]},{c:[{v: '3'}, {v: '3'}, {v: '4526'}]},{c:[{v: '4'}, {v: '4'}, {v: '4569'}]}]});  

            //instantiate our chart objects  
            var chart = new google.visualization.ColumnChart (document.getElementById('chart'));  

            //define options for visualization  
            var options = {width: 400, height: 240, is3D: true, title: 'Company Earnings'};  

            //draw our chart  
            chart.draw(data, options);  

        }  
    </script>

    <div id="chart"></div>

When using the code above the script is creating the graph, but something is wrong there

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:57:12+00:00Added an answer on May 26, 2026 at 1:57 am

    Per the docs, have you tried establishing the column references and data seperately?

    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Task');
    data.addColumn('number', 'Hours per Day');
    data.addRows([
      ['Work', 11],
      ['Eat', 2],
      ['Commute', 2],
      ['Watch TV', 2],
      ['Sleep', {v:7, f:'7.000'}]
    ]);
    

    To format into the correct JSON for the object, you can set it up as follows:

    while($r = mysql_fetch_assoc($sth)) {
       if(!isset($google_JSON)){    
         $google_JSON = "{cols: [";    
         $column = array_keys($r);
         foreach($column as $key=>$value){
             $google_JSON_cols[]="{id: '".$key."', label: '".$value."'}";
         }    
         $google_JSON .= implode(",",$google_JSON_cols)."],rows: [";       
       }
       $google_JSON_rows[] = "{c:[{v: '".$r['id']."'}, {v: ".$r['quarters']."}, {v: ".$r['salary']."}]}";
    }    
    // you may need to change the above into a function that loops through rows, with $r['id'] etc, referring to the fields you want to inject..
    echo $google_JSON.implode(",",$google_JSON_rows)."]}";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.