If that's what it is printing, then PHP isn't working on your server.

On Tuesday, December 17, 2013 11:09:58 AM UTC-5, Thomas Crowell wrote:
>
> Okay, it's printing simply the PHP code:
> <?php $con = mysql_connect('localhost', 'root', 'password') or die('Error 
> connecting to server'); mysql_select_db('myTest', $con); $query = 
> mysql_query('SELECT price, date FROM myTest'); $table = array(); 
> $table['cols'] = array( array('label' => 'price', 'type' => 'number'), 
> array('label' => 'date', 'type' => 'date') ); $rows = array(); while($r = 
> mysql_fetch_assoc($query)) { $temp = array(); $temp[] = array('v' => 
> $r['price']); $temp[] = array('v' => $r['date']); $rows[] = array('c' => 
> $temp); } $table['rows'] = $rows; // encode the table as JSON $jsonTable = 
> json_encode($table,JSON_NUMERIC_CHECK); header('Cache-Control: no-cache, 
> must-revalidate'); header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
> header('Content-type: application/json'); 
> header('Access-Control-Allow-Origin: *'); // return the data echo 
> $jsonTable; ?>
>
> Obviously that's not quite right. Any idea why it's doing that?
>
> On Tuesday, December 17, 2013 11:07:39 AM UTC-4, asgallant wrote:
>>
>> Add in the line console.dir(jsonData); between the AJAX call and the 
>> DataTable constructor, then open the page in Chrome or Firefox and open the 
>> developer's console.  You should see the JSON string printed there.
>>
>> On Tuesday, December 17, 2013 9:26:56 AM UTC-5, Thomas Crowell wrote:
>>>
>>> Hi,
>>>
>>> I'm fairly new to javascript, PHP, and SQL, and have been having trouble 
>>> getting my code to work. After searching for similar problems here and on 
>>> stackexchange, I haven't been able to fix it and thought I would post here 
>>> in hopes that someone could help.
>>>
>>> I'm trying to get some data from a MySQL database "myTest", convert it 
>>> into JSON format, and return it to draw an annotated timeline. When I test 
>>> my files in chrome, the console gives me the error "Uncaught 
>>> SyntaxError: Unexpected token <" at the line var data = new 
>>> google.visualization.DataTable(JSON.parse(jsonData));. In trying to fix 
>>> the issue, I believe the problem is likely that the PHP code is either 
>>> incorrect, or the return is not in JSON format, but I am unsure how to see 
>>> the output of the PHP to check what it looks like.
>>>
>>> Any help would be greatly appreciated, and I can try to provide any 
>>> other details that may be important in fixing the problem. My PHP code, and 
>>> a snippet of my javascript code is below.
>>>
>>> Thanks
>>>
>>> Querydata.php:
>>> <?php
>>> $con = mysql_connect('localhost', 'root', 'password') or die('Error 
>>> connecting to server');
>>>  
>>> mysql_select_db('myTest', $con); 
>>>
>>> $query = mysql_query('SELECT price, date FROM myTest');
>>>
>>> $table = array();
>>> $table['cols'] = array(
>>> array('label' => 'price', 'type' => 'number'),
>>> array('label' => 'date', 'type' => 'date')
>>> );
>>>
>>> $rows = array();
>>> while($r = mysql_fetch_assoc($query)) {
>>>     $temp = array();
>>>     $temp[] = array('v' => $r['price']);
>>>     $temp[] = array('v' => $r['date']);
>>>
>>>     $rows[] = array('c' => $temp);
>>> }
>>>
>>> $table['rows'] = $rows;
>>>
>>> // encode the table as JSON
>>> $jsonTable = json_encode($table,JSON_NUMERIC_CHECK);
>>>
>>> header('Cache-Control: no-cache, must-revalidate');
>>> header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
>>> header('Content-type: application/json');
>>> header('Access-Control-Allow-Origin: *');
>>>
>>>
>>> // return the data
>>> echo $jsonTable;
>>> ?>
>>>
>>>
>>> Test.js:
>>>
>>> function drawChart() {
>>>   var jsonData = $.ajax({
>>>               url: "http://localhost:8000/Desktop/Querydata.php";,
>>>               dataType:"json",
>>>       crossDomain : true,
>>>               async: false
>>>           }).responseText;
>>>   var data = new google.visualization.DataTable(JSON.parse(jsonData));
>>>   var chart = new 
>>> google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
>>>   chart.draw(data, {displayAnnotations: true});
>>> };
>>>
>>

-- 
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to