1

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

2

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

3 Comments

So I need to pass $row['GameNr'] variable to Flash. So I need to use $foo = $row['GameNr']?
Use echo $row['GameNr'] in your PHP
1

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!

Comments

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.