You have to format the data returned by your query into the JSON format 
expected by the DataTable constructor.  Here's a basic PHP script that you 
can work from:

<?php
/* $server = the IP address or network name of the server
 * $userName = the user to log into the database with
 * $password = the database account password
 * $databaseName = the name of the database to pull data from
 */
$con = mysql_connect($server, $userName, $password) or die('Error 
connecting to server');
 
mysql_select_db($databaseName, $con); 

// write your SQL query here (you may use parameters from $_GET or $_POST 
if you need them)
$query = mysql_query('SELECT column1, column2, column3 FROM myTable');

$table = array();
$table['cols'] = array(
/* define your DataTable columns here
 * each column gets its own array
 * syntax of the arrays is:
 * label => column label
 * type => data type of column (string, number, date, datetime, boolean)
 */
    array('label' => 'Label of column 1', 'type' => 'string'),
array('label' => 'Label of column 2', 'type' => 'number'),
array('label' => 'Label of column 3', 'type' => 'number')
// etc...
);

$rows = array();
while($r = mysql_fetch_assoc($query)) {
    $temp = array();
// each column needs to have data inserted via the $temp array
$temp[] = array('v' => $r['column1']);
$temp[] = array('v' => $r['column2']);
$temp[] = array('v' => $r['column3']);
// etc...
 // insert the temp array into $rows
    $rows[] = array('c' => $temp);
}

// populate the table with rows of data
$table['rows'] = $rows;

// encode the table as JSON
$jsonTable = json_encode($table);

// set up header; first two prevent IE from caching queries
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

// return the JSON data
echo $jsonTable;
?>

On Monday, February 25, 2013 3:58:55 PM UTC-5, akshita gupta wrote:
>
> Hello.. I am unable to display chart. Please help. I am unable to figure 
> out the problem. 
>
> getdata.php
>
> <?php
>
> mysql_connect('localhost','akshita','123456');
> mysql_select_db('rcusers');
>
> $sqlquery1="select userid,group_name,req_nodes,actualPE from jobs 
> <http://stackoverflow.com/questions/15073654/unable-to-extract-data-to-get-google-chart-nothing-is-being-displayed-but-getda#>
>  where userid='zhang' limit 200";
>
> $sqlresult1=mysql_query($sqlquery1);
>
> $rows=array();
> while($r=mysql_fetch_assoc($sqlresult1)){
>         $rows[]=$r;}print json_encode($rows);?>
>
> chartDraw.php
> <html><head><!--Load the AJAX API --><script type="text/javascript" 
> src="https://www.google.com/jsapi";></script><script 
> src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"; 
> type="text/javascript"></script>
> <script type="text/javascript">
> //Load the visualization API and the piechart package
> google.load('visualization','1',{'packages':['corechart']});
> //Set a callback to run when the google visualization API is loaded
> google.setOnLoadCallback(drawchart);
> function drawChart(){
>   var jsonData = $.ajax({
>         url:"getData.php",
>         dataType:"json",
>         async:false
>         }).responseText;
> //Create our data table out of JSON data loaded from servervar data=new 
> google.visualization.DataTable(jsonData);
> //Instantiate and draw our chart 
> <http://stackoverflow.com/questions/15073654/unable-to-extract-data-to-get-google-chart-nothing-is-being-displayed-but-getda/15073745#>,
>  passing in some optionsvar chart=new 
> google.visualization.PieChart(document.getElementById('chart_div'));
> chart.draw(data,{width:400,height:240});}
> </script></head>
> <body>
>         <!--Div that will hold the pie chart -->
>         <div id="chart_div"></div></body></html>
>
>
>
> On Tuesday, 26 July 2011 15:24:36 UTC-4, GerBen wrote:
>>
>> Hello Colleagues, 
>> How can I read data from an external database (say, MS SQL Server or 
>> MySQL) and show it in a Google Chart? 
>> Thank you.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Visualization API" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to google-visualization-api+unsubscr...@googlegroups.com.
To post to this group, send email to google-visualization-api@googlegroups.com.
Visit this group at 
http://groups.google.com/group/google-visualization-api?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to