Access a remote database from Adobe AIR or FLEX.
Many often you need to access online database from your application. For doing it in Adobe AIR of FLEX you need HttpService.
The following example shows how to access an online database from an AIR Application.
Note that you cannot return an array from a remote file or database, because the request is an asynchronous request. You can have a string as comma separated or some other separated which can be then send to our application. You can then split the string in your application according to your need.
Look at the example below which shows how to do this.
xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="init()"> id="sender" url="http://localhost/testDB/index.php" method="POST" resultFormat="text" result="resultHandler()" > xmlns=""> { "ID" } import mx.utils.StringUtil; import mx.rpc.events.FaultEvent; import mx.controls.Alert; public var result:String = ""; public function init():void{ sender.addEventListener(FaultEvent.FAULT,errorOccurred); sender.send(); //sending request to php file. } public function errorOccurred(event:FaultEvent):void{ trace('Invalid request'); Alert.show('Invalid request'); } public function resultHandler():void{ result = sender.lastResult.toString(); //get the result here from php as a string. trace(result); var arr : Array = new Array(); arr = result.split("||",result.length); arr.pop(); // remove the last blank element..... for(var i : int = 0; i < arr.length; i++){ trace(arr[i]); } } ]]>
The php part is given below
$con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("testDB", $con); $result = mysql_query("SELECT * FROM users"); while($row = mysql_fetch_array($result)) { echo $row['user_id']."||"; } mysql_close($con);