Part 4: More on Sending Data to/from Flash via PHP

In Part 2, we recorded an entry into the MySQL database. In Part 3, we essentially retrieved data from the MySQL database. In the final part of this tutorial, we will continue to explore how Flash/PHP/MySQL interact. Specifically, I will explain how to determine and display the ranking of the user at the end of each round, relative to all the records in the database.

Ranking of User relative to records in database

Let’s get started. First, Flash sends data to the PHP script. The PHP script then queries the MySQL database and obtains the information desired. This information is returned to the flash application, informing the user what rank was received relative to all the records in the database. The flow chart below might help you visualize the process.

Ranking of User relative to records in database

  1. Open up a new Flash File (Actionscript 3.0). Press F9 and paste the following code into the Actions – Frame. This code uses URL variables to send data to the PHP script.
    import fl.controls.TextInput;
    import fl.controls.TextArea;
    var variables:URLVariables = new URLVariables();
     
    //specifies the URL variables that will be used
    variables.playerTime = 1.03;
    variables.roundNum = "roundOneTime"; 
     
    var request:URLRequest = new URLRequest();
     
    ////insert in the location of the php script //////////
    request.url = "insert web address of php file here";
    ///////////////////////////////////////////////////////
     
    request.data = variables;
     
    var loader:URLLoader = new URLLoader();
    loader.load(request);
     
    //after event is completed, goes to the function displayRanking
    loader.addEventListener(Event.COMPLETE,displayRanking)
     
    function displayRanking(e:Event):void {
    	//creates a textbox to display text data
    	var displayRank:TextField = new TextField;
    	displayRank.autoSize = "center";
    	displayRank.x =200;
    	displayRank.y= 200;
    	displayRank.text = e.target.data;
    	addChild(displayRank);
    }
  2. The following script is used to determine the ranking of the current player after every round. Copy and paste this code, and save it as a php file. Remember to fill in the information for the host, username, password, etc.
    <?php
     
    /////////////////////// fill in this information here /////////////////////
      $host = "";
      $user = "";
      $pass = "";
      $database = "";
      $table = "";
    /////////////////////////////////////////////////////////////////////////////
     
    //get the URL variables and store them into PHP variables 
    //that can be used here in this script
    $playerTime = $_GET['playerTime']-.0001; //to correct precision problems
    $round = $_GET['roundNum']; 
     
    $linkID = mysql_connect($host,$user,$pass) or die("Could not connect to host.");
    mysql_select_db($database, $linkID) or die("Could not find database.");
     
    $query="SELECT * FROM {$table}";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    $num= $num+1;   //this variables holds the number of entries in the database + 1
     
    $query2= "select * from {$table} where {$round} < {$playerTime}";
    $result2= mysql_query($query2,$linkID) or die("Data not found.");
    $rank = mysql_numrows($result2); 
    $rank = $rank + 1;  //this variable holds the #of lower than the current time +1
     
    echo("You are rank {$rank} out of {$num} for this round!");
    ?>
  3. Next, upload the PHP file onto your website. Also, be sure to fill in the following line correctly on the flash code:

    ////insert in the location of the php script //////////
    request.url = “insert the web address of php file here”;
    ///////////////////////////////////////////////////////

  4. Now, go to Adobe Flash, and run the application. If you did everything correctly, then you should see the following:

    You are rank 211 out of 443 for this round!

    You can go back into the code and change the playerTime variable to confirm that the ranking changes with different times.

  5. If the information above doesn’t appear, then that means that you have either imported the table incorrectly, did not assign the correct privileges to the user, or your information in the php file is incorrect.
  6. So what exactly is going on here? Essentially, Flash is sending URL variables to the PHP script, which in turn queries the MySQL database. The MySQL database returns the requested data to the PHP script, which then goes back to the Flash application to be displayed.

End of Tutorial

Pages: 1 2 3 4 5