0

I was wondering if some one could explain how I can read a MySQL result array in AS3.

I am using:

var loader:URLLoader = new URLLoader();
var urlReq:URLRequest = new URLRequest("http://domain.com/get-api.php");
var urlVars:URLVariables = new URLVariables();
loader.dataFormat = flash.net.URLLoaderDataFormat.TEXT;
urlReq.method = URLRequestMethod.POST;
urlReq.data = urlVars;
urlVars.mySubmittedRateID = GlobalVariables.mySubmittedRateID;
urlVars.myPostcode = GlobalVariables.currentPostcode;
loader.addEventListener(flash.events.Event.COMPLETE, getDataOnComplete);
loader.load(urlReq);

to pass a parameter (2 in fact) to the get-api.php file. In the file I PHP reading in the parameters and a normal SQL select where statement which gets all the results matching the parameters.

My getDataOnComplete function is:

private function getDataOnComplete(event:flash.events.Event):void
{
    var loader:URLLoader = new URLLoader(event.target);
    var resultsArray:Array = JSON.decode(loader.data);
}

My PHP:

$row_array['done'] = "true";
return json_encode($row_array);

Error:

Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs.
at Error$/throwError()
at flash.net::URLVariables/decode()
at flash.net::URLVariables()
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

For some reason I cant even get to the getDataOnComplete function.

0

1 Answer 1

0

Your php mysql result will be array then

In php side you need to convert into result array into json_encode($result_array) like

echo json_encode($result_array); //Note here not return statement;

In as3 side

you can use latest AIR SDK having native JSON class (Here using AIR class) or as3corelib to parse JSON data.

private function getDataOnComplete(event:flash.events.Event):void
{
  var resultObj:Object = event.target.data;
  var resultsArray:Object = JSON.stringify(resultObj);
}

For more details pass-an-array-from-php-to-actionscript-3

Sign up to request clarification or add additional context in comments.

6 Comments

And then a for loop in AS3 to get each record?
I get an error: Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs. at Error$/throwError() at flash.net::URLVariables/decode() at flash.net::URLVariables() at flash.net::URLLoader/onComplete()
For avoid those error change loader.dataFormat = URLLoaderDataFormat.VARIABLES to loader.dataFormat = URLLoaderDataFormat.TEXT.
I am still getting the same error. I have updated my question with the latest code.
Check in php side not return just simply use echo statement. and also getDataOnComplete() bit changed it works for me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.