Coding By Google – AS3 to PHP with Simple MYSQL DB Query and Data Return Tutorial (Free Code)

By Jeff Fulton (@8bitrocket on twitter)

Coding By Google – AS3 to PHP with Simple MYSQL DB Query and Data Return Tutorial (Free Code)

Here at Producto Studios, I use Google to look up code all day long.  Yes, I have written books on Javascript, Flash, Actionscript, and HTML5, but I still find myself looking stuff up all the time. There just is too much to know when trying to fulfill the requirements of a project.

So, a few days ago a needed to Call a PHP script from AS3, hit MySql,  and return back some data.  Nothing fancy, but not something I have not done in a while (since my Zynga days). I used a couple different resources to cobble together the code, but not any one that had everything I needed.  so I thought in the sense a fairness in sharing,  I would present this basic version (that could probably be much more secure, etc) in case anyone needed a starter in how to accomplish this task.

The PHP

I am by no means a PHP superstar.  I grew up on Perl, moved to vbscript, then to .net, then hid from server side coding until I had to learn a little PHP a couple years back. I think it is an excellent language though and use it as often as I can.

Our example is going to accept in an email address, check to make sure it is a valid email (hopefully this will solve most SQL injection problems, but if there is a better way, please let me know).  It will then look up a user in the MySQL Database with the email address. If the user exists, it will pass back the first and last name of the user.  Since this is pure PHP, any script, even JQUERY could call it and use it.  For this example though, Flash AS3 is going to be the m method used.

The Server Side PHP Script

You will have to provide your own database IP address or DNS name, along with the user, pass and db name.

  1. <?php
  2. $email = $_REQUEST['email'];
  3. $returnString="retval=";
  4. if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  5.    $mysqli= new mysqli("xxx.xxx.xxx.xxx", "user", "pass", "db");
  6.    $sql="SELECT *FROM `user` WHERE email = '".$email."'";
  7.    $res=mysqli_query($mysqli, $sql);
  8.    if ($res) {
  9.       while ($newArray=mysqli_fetch_array($res, MYSQLI_ASSOC)) {
  10.       $first = $newArray['first'];
  11.       $last = $newArray['last'];
  12.       $returnString=$returnString."&first=".$first."&last=".$last;
  13.    }
  14.    echo $returnString;
  15. }
  16.    mysqli_free_result($res);
  17.    mysqli_close($mysqli);
  18. }else{
  19.    echo $returnString;
  20. }
  21. ?>

The Client Side Actionscript

This is the AS3 version of the script used to call the PHP.

 

  1. package {
  2.   import flash.events.*;
  3.   import flash.net.URLLoader;
  4.   import flash.net.URLRequest;
  5.   import flash.net.URLVariables;
  6.   import flash.net.URLLoaderDataFormat;
  7.   import flash.net.URLRequestMethod;
  8.   import flash.display.MovieClip;
  9.   public class Main extends MovieClip{
  10.     private var EMAIL_REGEX: RegExp = /^[A-Z0-9._%+-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
  11.     private var formattedEmailAddress:String;
  12.     public function Main() {
  13.       trace("test as3 to php to mysql and back");
  14.       var email="Teff@Test.com";
  15.       formattedEmailAddress=email.toLowerCase();
  16.       if (formattedEmailAddress.match(EMAIL_REGEX)) {
  17.         trace("good email address format");
  18.         trace("checking database for email address...");
  19.         checkDbForEmail();
  20.       }else{
  21.         trace("bad email address format");
  22.       }
  23.     }
  24.     private function checkDbForEmail():void {
  25.       var url="http://addresstoyourserver/doesemailexist_tutorial.php";
  26.       var loader : URLLoader = new URLLoader;
  27.       var urlreq:URLRequest = new URLRequest(url);
  28.       var urlvars: URLVariables = new URLVariables;
  29.       loader.dataFormat = URLLoaderDataFormat.VARIABLES;
  30.       urlreq.method = URLRequestMethod.POST;
  31.       urlvars.email = formattedEmailAddress;
  32.       urlreq.data = urlvars;
  33.        loader.addEventListener(Event.COMPLETE, completed);
  34.       trace("calling loader");
  35.       loader.load(urlreq);
  36.     }
  37.     private function completed(event:Event): void{
  38.       trace("php event") ;
  39.       var loader2: URLLoader = URLLoader(event.target);
  40.       trace("first=", loader2.data.first);
  41.       var firstname:String=loader2.data.first;
  42.       var lastname:String=loader2.data.last;
  43.       if (firstname == null || firstname =="") {
  44.         trace("Sorry, that email does not exist in our database");
  45.       }else{
  46.        trace("user exists");
  47.       }
  48.     }
  49.   }
  50. }

That’s all there is to it. I’m not christening this as the only or the best way to accoimplish this task, just one way that works and is moderately secure.  If you have a better way, please sent it over and I’ll credit you and post your additions and comments.

Jeff Fulton is the Chief Technology Officer at Producto Studios, and co-author of The Essential Guide To Flash Games,as well as The HTML5 Canvas 1st and 2nd editions.
He can be reached at info[at]8bitrocket.com and at the @8bitrocket twitter address.

0saves
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.
This site is protected by Comment SPAM Wiper.