tags:

views:

908

answers:

2

I am trying to return results from my SQL database, using PHP to convert it into JSON, which is then read by Flex.

Here is the parse error I receive -

JSONParseError: Unexpected < encountered
at com.adobe.serialization.json::JSONTokenizer/parseError()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONTokenizer.as:579]
at com.adobe.serialization.json::JSONTokenizer/getNextToken()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONTokenizer.as:168]
at com.adobe.serialization.json::JSONDecoder/nextToken()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONDecoder.as:83]
at com.adobe.serialization.json::JSONDecoder()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSONDecoder.as:60]
at com.adobe.serialization.json::JSON$/decode()[/Users/mesh/src/as3corelib/src/com/adobe/serialization/json/JSON.as:78]
at main/getPHPData()[C:\wamp\www\ClassDB\src\main.mxml:25]
at main/__getData_result()[C:\wamp\www\ClassDB\src\main.mxml:58]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.rpc.http.mxml::HTTPService/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\http\mxml\HTTPService.as:290]
at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:193]
at mx.rpc::Responder/result()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:43]
at mx.rpc::AsyncRequest/acknowledge()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:74]
at DirectHTTPMessageResponder/completeHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:403]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

And here is the PHP I am using -

if(isset($_GET['getclassdb']))
{
  mysql_connect($URL, $USERNAME, $PASSWORD);
  mysql_select_db($DATABASE) or die('Cannot connect to database.');
$returnArray = array();
$query = 'SELECT * FROM classdb';
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result))
{
array_push($returnArray, $row);
}

mysql_close();
echo json_encode($returnArray);
}
elseif(isset($_GET['setclassdb']))
{
$jsonString = urldecode($_GET['jsonSendData']);
$jsonString = str_replace("\\", "", $jsonString);
$data = json_decode($jsonString, true);

mysql_connect($URL, $USERNAME, $PASSWORD);
mysql_select_db($DATABASE) or die('Cannot connect to database.');

foreach ($data as $classdbEntry) {
$query = sprintf(
'UPDATE Tutorials SET rating = "%s" WHERE id = "%s"',
mysql_real_escape_string($classdbEntry['rating']),
mysql_real_escape_string($classdbEntry['id']));

$result = mysql_query($query);

if(!$result)
{
mysql_close();
echo mysql_error();
return;
}
}
mysql_close();
echo "database updated";
}
?>

I am at a complete loss at where to go from here. Any help or hints in the right direction would be greatly appreciated!

A: 

Did you try opening the php in a browser and see if the output is in proper json syntax... if you are using firefox, then JSONView add-on should be of help in finding json errors..

Shrikant Sharat
A: 

I believe the problem is that you are not "returning" (printing out) valid json, which is what the actionscript class is expecting. As far as I can tell your PHP script either returns nothing at all, or the string "database updated" neither of which is valid JSON.

I don't know if the actionscript side has particular expectations about what it will receive. If it doesn't, simply printing out the string "{}" might be enough; that would be an empty JSON object.

Jay Paroline