Php – JSON Parse Error using Adobe Flex – PHP – SQL

apache-flexjsonPHPsql

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 –

<?php

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!

Best Answer

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..