How to pass variables from PHP to Flash (ActionScript 3)

I need to select game's number from MySql database and pass It to Flash game (ActionScript 3).

For now I have AS3 code:

function getVars(e:Event):void{
    var req:URLRequest = new URLRequest("top.php");
    var loader:URLLoader = new URLLoader(req);
    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.addEventListener(Event.COMPLETE, getVarComplete);
}

function getVarComplete(e:Event):void{
    var gameNr.text = e.target.data;
}

And here is PHP code (I don't know how selected row pass to variable and send It to flash)

$id = $_SESSION['id']; 
$mysqli = new mysqli("localhost","xxx","xxx","xxx");

$query = "SELECT GameNr 
          FROM   Game 
          WHERE FBId = '". mysql_real_escape_string($id) ."' 
          ORDER BY PlayTime DESC LIMIT 1";

UPDATE:

If I use following PHP:

<?php 
    session_start();
    $id = $_SESSION['id']; 

    $mysqli = new mysqli("localhost","xx","xx","xx");

    $query = "SELECT GameNr 
              FROM   Game 
              WHERE FBId = '". mysql_real_escape_string($id) ."'              
              ORDER BY PlayTime DESC LIMIT 1";
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
} 

    if ($result = $mysqli->query($query)) {
        while ($row = $result->fetch_assoc()) {
           echo $row["GameNr"];
        }       
        $result->free();
    }
    $mysqli->close();
?>

www.myhost.com/top.php returning correct value NA==


If I use following AS3 code:

function onVarsLoaded(e:Event) {
    var msg:String = "Communication with the server was successful.\n\n";
    msg += "foo -> "+e.target.vars.foo+"\n";
    trace(msg);
}

It returning me: foo -> undefined

If I change this

msg += "foo -> "+e.target.vars.foo+"\n"; to

msg += "foo -> "+e.target.vars+"\n";

It returning me incorrect value: foo -> NA=%3D

Upvotes: 1

Views: 900

Answers (2)

user1646615
user1646615

Reputation:

Try this:

AS3:

import net.kaegi.loaders.VarLoader;
var vl:VarLoader;

sendBtn.addEventListener(MouseEvent.CLICK,sendBtnHandler);
function sendBtnHandler(e:MouseEvent) {
    // Variables sent by POST-Method:
    var varObj:Object = {};
    varObj.textinput0 = escape(textinput0.text);
    varObj.textinput1 = escape(textinput1.text);

    vl = new VarLoader("http://yourserver.com/landing.php?foo=foo", varObj);
    vl.addEventListener(Event.COMPLETE, onVarsLoaded);
    vl.addEventListener(Event.CANCEL, onVarsCancel);
}

function onVarsLoaded(e:Event) {
    var msg:String = "Communication with the server was successful.\n\n";
    msg += "foo -> "+e.target.vars.foo+"\n";
    tf_servermsg.textColor = 0x009900;
    tf_servermsg.text = msg;
}

function onVarsCancel(e:Event) {
    tf_servermsg.textColor = 0x990000;
    tf_servermsg.text = e.target.errormsg;
}

PHP

// handle coming get
$foo = $_GET["foo"];

// handle coming post from flash
$textinput0 = $_POST["textinput0"];
$textinput1 = $_POST["textinput1"];

// send it to flash
$yourdata = "hello world"; // your mysql data here


echo $yourdata;

Upvotes: 1

Amit Vikram
Amit Vikram

Reputation: 402

PHP code in addition to your code

 $mysqli->real_query($query);
 $res = $mysqli->use_result();
 while ($row = $res->fetch_assoc()) {
     echo $row['GameNr'];
}

Please check in format the data is required from PHP script. currently the php code will give you result of GameNr as text.

Hope it helps!

Upvotes: 1

Related Questions