Flash AS3 Tutorials Center | Interesting Flash Animation Tutorials | Valuing Flash Animation Tutorials | Flash ActionScript and Animation Home

AS3 Beginner Tutorials | AS3 Basic Lessons | AS3 Valuing Courses | AS3 Components Tutorials | AS3 and PHP Interaction Tutorials
AS3 Practical Tutorials | AS3 Animation Techniques | AS3 Transition Effects Tutorials | AS3 Download Upload Files | AS3 Particle Systems
Communication Between Flash Movies with AS3 | AS3 and JavaScript interaction | AS3 Matrix Transformation | AS3 Physics Simulation Tutorials

Basic Example of Flash ActionScript PHP MySQL Communication

This is a basic example of Flash ActionScript and MySQL database communication.It shows how data pass between them.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

This is rather easy for Flash ActionScript to communicate with PHP and MySQL database. With this knowledge, a lot of useful flash application can be developed.

The Flash Movie of this tutorial is shown as above.

Process of ActionScript PHP and MySQL Interaction

  1. Flash Actionscript send a username variable (alex) to PHP
  2. PHP connect MySQL database, select the required database table, create and exexute query from the table that meet the criteria.
  3. Records that meet the criteria are sent back to PHP.
  4. PHP return the data back to Flash ActionScript.

MySQL DataBase Table Records

Here's the database records used in this tutorial:

MySQL database

PHP File Codes:

<?php

$username = $_POST['username'];

// create connection
$connection = mysql_connect("localhost", "db_user", "db_password") or die ("Couldn't connect to the server.");

// select database
$db = mysql_select_db("db_name", $connection) or die ("Couldn't select database.");

// create SQL
$sql = "SELECT domain FROM db_table where username = '$username'";

// execute SQL query and get result
$sql_result = mysql_query($sql, $connection) or die ("Couldn't execute query.");

// get number of rows in $result.
$num = mysql_numrows($sql_result);

$phpConfirm = "";

$counter = 0;

while ($row = mysql_fetch_array($sql_result)) {

$domain = $row["domain"];
if ($counter == 0) {
$phpConfirm .= $domain;
} else {
// Use a item limiter "|" to seperate the records
$phpConfirm .= "|" . $domain;
}

$counter++;
}

echo "phpConfirm=" . $phpConfirm . "&totalItem=" . $num;

// free resources and close connection
mysql_free_result($sql_result);
mysql_close($connection);

?>

There are two variables return back to Flash ActionScript:

- phpConfirm

- totalItem

Flash ActionScript Codes:

function checkComplete(evt:MouseEvent):void {

// Create A new URLVariables instance to store the variable
var myVariables:URLVariables = new URLVariables();

// Create a variable (e.g. username) to send
myVariables.username = candidate_txt.text;

// Create a new URLRequest instance sending data to "ascom01.php"
var myRequest:URLRequest = new URLRequest("apm01.php");

// Send data using the POST method
myRequest.method = URLRequestMethod.POST;

// The data property of the request is set to the
// URLVariables instance (myVariables) to send to the PHP file.
// Note: myVariables stored the variable (e.g. candidate)
myRequest.data = myVariables;

// Create a new instance of the URLLoader class to work with.
// URLLoader.load( ) method should be used when we need the
// sent variables returned back to Flash ActionScript.
var myLoader:URLLoader = new URLLoader;

//specify dataFormat property of the URLLoader to be "VARIABLES"
//This ensure that the variables loaded into Flash with the same variable names
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;

//Load the PHP file by using URLRequest
myLoader.load(myRequest);

//Listen when the loading of data COMPLETE
//Call the loadComplete function when the loading COMPLETE
myLoader.addEventListener(Event.COMPLETE, loadComplete);


}

 

// Hook up the button with the function checkComplete
enter_btn.addEventListener(MouseEvent.CLICK, checkComplete);


// This is the function that display the data returned back from PHP file
function loadComplete(evt:Event):void {


//Display the value with variable name "totalItem"
total_txt.text = evt.target.data.totalItem

//Get the value (string) with variable name "phpConfirm"
var myResult:String = evt.target.data.phpConfirm;

//Split the string into an Array
var myArray:Array = myResult.split("|");
//output_txt.text = "The number of items are: " + myArray.length;

var finalString = "";
var i:int;

for (i = 0; i < myArray.length; i++) {

finalString = finalString + myArray[i] + "<br>";

}

output_txt.htmlText = finalString;



}

 

Download Flash Source File:

Flash Source File as3-php-mysql-01.fla

Remarks:

This Flash ActionScript tutorial shows how to use Actionscript to communicate with PHP and MySQL.