shelfish
shelfish

Reputation: 73

AS3, PHP, mysql: Cant get variables from server

Im using as3, mysql and php, trying to send username and receive (points) and post the points in a text object on the stage, I can send points to the server with similar script but can get numbers back!


// global variables
var loader:URLLoader;
var urlReq:URLRequest;
var urlVars:URLVariables;
var myVars:URLVariables;


addEventListener(Event.ENTER_FRAME, onit);

function onit(e:Event):void
{


    urlReq = new URLRequest("http://www.mysite.com/GetPoints.php");
    urlReq.method = URLRequestMethod.POST;


    urlVars = new URLVariables();
    urlReq.data = urlVars;
    urlVars.username = nickname_txt.text;



    loader = new URLLoader(urlReq);
    loader.addEventListener(Event.COMPLETE, onUpdateComplete);


    loader.dataFormat = URLLoaderDataFormat.VARIABLES;
    loader.load(urlReq);
}

function onUpdateComplete(e:Event):void
{
    myVars = e.target.data;
    totalP.text = myVars.total;
}

the php reruns the points when executed in a browser, but not connecting to flash.

<?php
$username = $_POST["username"];
    // defining main variables
    $dbHost = "localhost";
    $dbUser = "";
    $dbPass = "";
    $dbName = "";
    $dbTable = "`".$username."_points`";


    @mysql_connect($dbHost, $dbUser, $dbPass) or die(mysql_error());
    @mysql_select_db($dbName) or die(mysql_error());


    $data = "";
    $res = mysql_query("SELECT * FROM ".$dbTable." ORDER BY id") or die(mysql_error());
    while($row = mysql_fetch_object($res)) {


        $data = "total=".$row->total;
        print $data;
    }

?>

Any help would be greatly appreciated !

Upvotes: 0

Views: 1174

Answers (2)

Rajneesh Gaikwad
Rajneesh Gaikwad

Reputation: 1193

Try adding something like this to your PHP,

 $someVar  = "l=l";
 $someVar .= "&someData=".rawurlencode($someData);
 echo $someVar;

Best luck.

Upvotes: 0

Joe Slater
Joe Slater

Reputation: 2473

You have to echo or println your data in php.

The format in which you have to print it is as follows -

key1=value1&key2=value2&key3=value3 and so on

Then you should be able to access the data in AS3 like this -

myVars = e.target.data;
totalP.text = myVars.key1; //this will be value1

Also the first variable you pass should not have an ampersand before it.

What I suggest you do is something like this

print "key1=value1"; //print out a test value    
while($row = mysql_fetch_object($res)) {


        $data = "&total=".$row->total; //add an ampersand before the key
        print $data;
    }

Upvotes: 1

Related Questions