0

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 !

5
  • You have to echo or println your data in php. You can have a look at similar type of code I have done here - stackoverflow.com/questions/13828930/…, not the question specifically but the code that I used could be useful for you. Commented Apr 4, 2013 at 20:12
  • added: print $row["total"]; but i still get:Error: Error #2101: The String passed to URLVariables.decode() must be a URL-encoded query string containing name/value pairs. at Error$/throwError() Commented Apr 4, 2013 at 20:18
  • Do you have a ampersand between? You must print your data out in the following format key1=value1&key2=value2&key3=value3 and so on. Commented Apr 4, 2013 at 20:19
  • thanks Ankur, changed to $data = "total=".$row->total; print $data; and if I put the username in by hand it works. so not passing the username properly now Commented Apr 4, 2013 at 21:18
  • If it worked can you plz accept the answer? Also the first variable you pass should not have an ampersand before it. Commented Apr 5, 2013 at 6:40

2 Answers 2

1

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

Comments

0

Try adding something like this to your PHP,

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

Best luck.

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.