error 1046 JSON string

54 views
Skip to first unread message

Razee Hussein-Jamal

unread,
Aug 12, 2013, 3:19:07 PM8/12/13
to google-visua...@googlegroups.com

 I am trying to view my JSON string I receive from a database and view it in google tables, however I receive this error: Invalid JSON string: error: 1046

 In viewTable.php a part of the script is :

  function drawVisualization() {
     
        var jsonData = $.ajax({
          url: "json.php", // make this url point to the data file
          dataType: "json",
          async: false
        }).responseText;
    var data = new google.visualization.DataTable(jsonData);
  
      visualization = new google.visualization.Table(document.getElementById('table'));
      visualization.draw(data, null);
            
    }

in json.php file : I query data from database and encode it to JSON string which I then echo $jsonString,

An example of JSON string output :


{"cols":
[{"id":"A","label":"Date","type":"string"},{"id":"B","label":"User","type":"string"},{"id":"C","label":"Cement Brand","type":"string"},{"id":"D","label":"Volume","type":"string"},{"id":"E","label":"Type","type":"string"}],

"rows":
[{"c":[{"v":"08-06-2013"},{"v":"raze...@gmail.com"},{"v":"Muthana"},{"v":"27"},{"v":"Local Plant"}]}]
}


I would really appreciate any help with this problem as I can't find the reason for this invalid JSON string as it seems to be correct. Thank you






asgallant

unread,
Aug 12, 2013, 4:24:29 PM8/12/13
to google-visua...@googlegroups.com
I don't get that error when I use your JSON: http://jsfiddle.net/asgallant/R8tA9/.  Can you post an example script (preferably on jsfiddle, you can use the example I linked to as a starting point) that replicates the issue?

Razee Hussein-Jamal

unread,
Aug 16, 2013, 8:37:56 AM8/16/13
to google-visua...@googlegroups.com
Thank you for your help, this is the code I have inside the json.php the connection to database is done in a separate file, I don't get the JSON objects when I call this file in the ajax script.

<?php

$selectQuery = 'SELECT * FROM competitive_daily_volume';
$result = mysql_query($selectQuery);
$rows = array();
if($result){
    
    while($row = mysql_fetch_assoc($result)){
        $temp =  array();
        $temp[] = array("v"=> $row['Date']);
        $temp[] = array("v"=> $row['UserId']);
        $temp[] = array("v"=> $row['CementBrand']);
        $temp[] = array("v"=> $row['VolumeBGLP']);
        $temp[] = array("v"=> $row['Type']);
        
        $rows[] = array("c"=> $temp);

    }

}

$table = array();
$table['cols'] = array(
    array("id"=>"A","label"=>"Date","type"=>"string"),
    array("id"=>"B","label"=>"User","type"=>"string"),
    array("id"=>"C","label"=>"Cement Brand","type"=>"string"),
    array("id"=>"D","label"=>"Volume","type"=>"string"),
    array("id"=>"E","label"=>"Type","type"=>"string")
);
  
$table['rows'] = $rows;
$jsonObj = json_encode($table);

echo $jsonObj;

?>



--
You received this message because you are subscribed to a topic in the Google Groups "Google Visualization API" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-visualization-api/_4eMVS6Hdnk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-visualizati...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Razee Hussein-Jamal
Masters in Embedded Systems,
Department of Information Technology,
Uppsala University - Sweden
Mobile Phone : +46764158553 (Sweden) , +9647701524721 (N.Iraq), +905058622477 (Turkey)

asgallant

unread,
Aug 16, 2013, 10:43:53 AM8/16/13
to google-visua...@googlegroups.com
Your PHP code looks fine.  Navigate to that script in a browser and see if it is outputting a JSON string there.
To unsubscribe from this group and all its topics, send an email to google-visualization-api+unsub...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Razee Hussein-Jamal

unread,
Aug 16, 2013, 11:52:32 AM8/16/13
to google-visua...@googlegroups.com
The JSON string is : {"cols":[{"id":"A","label":"Date","type":"string"},{"id":"B","label":"User","type":"string"},{"id":"C","label":"Cement Brand","type":"string"},{"id":"D","label":"Volume","type":"string"},{"id":"E","label":"Type","type":"string"}],"rows":null}

using firebug. 

I don't really understand why cause when I only call the dataTableViewDaily.php echo jsonObj it prints the row as well.

What could the reason for this be?


To unsubscribe from this group and all its topics, send an email to google-visualizati...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.

Razee

unread,
Aug 16, 2013, 4:17:57 PM8/16/13
to google-visua...@googlegroups.com
It seems that it doesn't enter the while loop when this file is called from the JavaScript. Could it be that the 
while($row = mysql_fetch_assoc($result)) 

Blocks the Ajax  when it's called, the rows on the Json string is null but cols is not and its correct when it's been passed. 

I really need help with this part I have been working on it for days, there is a mistake which I can't find. 
To unsubscribe from this group and all its topics, send an email to google-visualizati...@googlegroups.com.

Razee

unread,
Aug 16, 2013, 4:23:55 PM8/16/13
to google-visua...@googlegroups.com
I should also mention I have changed this line of code : 

$selectQuery = 'SELECT * FROM competitive_daily_volume';
$result = mysql_query($selectQuery);
$rows = array();
if($result){
    
    while($row = mysql_fetch_assoc($result)){..........} 
}

To this:

$selectQuery = 'SELECT * FROM competitive_daily_volume';

$rows = array();
if($result = mysql_query($selectQuery)){
    
    while($row = mysql_fetch_array($result)){ ........}

}

But the rows is null, I don't think it enters the while loop when this file has been called from the JavaScript. 

Would appreciate any kind of help, thank you in advance 
Sent from my iPhone

asgallant

unread,
Aug 16, 2013, 9:03:33 PM8/16/13
to google-visua...@googlegroups.com
I don't think that would change anything.  Calling the PHP script from an AJAX call also shouldn't return any different results.  If it works right when you visit the URL directly from your browser, it should work when you pull it from AJAX.  The only time you might expect a different result is if the domain of the HTML file making the AJAX call is different from the domain of the data source script.

Razee Hussein-Jamal

unread,
Aug 17, 2013, 4:50:46 AM8/17/13
to google-visua...@googlegroups.com
I tried to run the PHP code directly from the URL it worked fine, it returned the JSON string correctly but once again I tried to call this PHP code from AJAX and the row returned to null, as it's not entering the while loop as I mentioned earlier. The domain is the same of the AJAX script and data source script is the same....
This is driving me crazy!

If you could please look at my code maybe I am missing sth, http://stackoverflow.com/questions/18274356/display-google-chart-with-json-and-php




To unsubscribe from this group and all its topics, send an email to google-visualizati...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.

Razee Hussein-Jamal

unread,
Aug 17, 2013, 7:37:57 AM8/17/13
to google-visua...@googlegroups.com
I found the problem,

My database connection is established on a seperate php file which is included into index.php, form index.php file the system navigates to different pages for example the viewDataTable.php which has the ajax script that calls the php code for query data from database. However even if the database connection is established globally to all the pages, the connection is lost once the ajax script runs.

So I had to include it once again, but wonder if there are any other ways to solve this?

Thank you!

asgallant

unread,
Aug 17, 2013, 11:39:26 AM8/17/13
to google-visua...@googlegroups.com
From a design perspective, I prefer to open database connections only when strictly necessary, and close them when I'm done using them.  You could, I believe, start a session on your index page and save your database connection in a $_SESSION variable (then retrieve the database connection from $_SESSION in the viewDataTable.php script), but this could result in an arbitrary number of database connections hanging open on your server until the sessions are closed or timed out.  I think you are asking for problems at that point.
To unsubscribe from this group and all its topics, send an email to google-visualization-api+unsubscr...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Razee Hussein-Jamal
Masters in Embedded Systems,
Department of Information Technology,
Uppsala University - Sweden
Mobile Phone : +46764158553 (Sweden) , +9647701524721 (N.Iraq), +905058622477 (Turkey)

--
You received this message because you are subscribed to a topic in the Google Groups "Google Visualization API" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-visualization-api/_4eMVS6Hdnk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-visualization-api+unsubscr...@googlegroups.com.

To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to a topic in the Google Groups "Google Visualization API" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-visualization-api/_4eMVS6Hdnk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to google-visualization-api+unsub...@googlegroups.com.
To post to this group, send email to google-visua...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-visualization-api.
For more options, visit https://groups.google.com/groups/opt_out.



--
Razee Hussein-Jamal
Masters in Embedded Systems,
Department of Information Technology,
Uppsala University - Sweden
Mobile Phone : +46764158553 (Sweden) , +9647701524721 (N.Iraq), +905058622477 (Turkey)

Reply all
Reply to author
Forward
0 new messages