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

Another Example of Flash ActionScript PHP MySQL Communication

This is a more practical example of Flash ActionScript and MySQL database communication. The user name can be selected from a drop down box.

Please update flash player to view this Flash ActionScript tutorial!

Flash Tutorial Content:

This Flash ActionScript PHP and MySQL communication tutorial shows how to use drop down box to send variables to PHP and MySQL.

The Flash Movie of this tutorial is shown as above.

MySQL DataBase Table Records

We are using the the same database records in this tutorial:

MySQL database

PHP File Codes:

The PHP codes are same as previous example:

<?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:

//Add Items to ComboBox
memberCombo.prompt = "Please select a user";
memberCombo.addItem( {label: "alex" } );
memberCombo.addItem( {label: "kathy" } );
memberCombo.addItem( {label: "peter" } );

 

//Add CHANGE event listener to ComboBox
//Whenever the index of Combox CHANGE, function checkComplete will be called
memberCombo.addEventListener(Event.CHANGE, checkComplete);

 

function checkComplete(evt:Event):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 = evt.target.value;

// 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);


}


// 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-02.fla

Remarks:

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