Beginners Flash Tutorial – Working with MYSQL and PHP
October 30, 2008 by admin
Filed under Flash Tutorials, PHP
High Quality – Flash Tutorial Videos – Taught by experts.
We also have extensive Adobe Flash Tutorials in high quality video format. These are ideal for beginners who need to master Flash quickly
Title / Free Demo : Adobe Flash CS3 Tutorial Videos
Author: James Gonzalez
Duration: 11 Hours – Lessons: 125
Flash with PHP / MySQL
The code used in this tutorial is mainly ActionScript 2.0, with notes for using ActionScript 3.0 – basic knowledge of databases (particularly MySQL) and server-side scripting concepts is assumed, for users who require an a tutorial on Beginners PHP and MySQL we recomend the Beginners PHP Training Video
Given that Flash is used primarily on the Web, database driven components are a common requirement. In order to use data in this way within your Flash movies, you will need to use some sort of server-side scripting language, such as PHP. We use server-side scripting to communicate with a data source on the server, and Flash has some built-in capabilities for handling the data at the client-side.
To demonstrate, we will create a simple database-driven Flash movie using PHP and MySQL. This example will need to be deployed on a server with PHP and MySQL installed.
In MySQL, create a database called ‘flashtest’, with one table in it called ‘flashdata’; in the table create just one field (for simplicity – the details of the database are not important), name the field ‘flash_text’ and choose VARCHAR as the data type. Enter a few rows of data into the table, any strings of text will be fine.
Now we need PHP code to connect to and query your database. Create a file called get_data.php in the directory you intend to put your Flash file in – this will need to be on the same server as the MySQL database for the following code to work. In get_data.php enter the following code (replacing username and password with your own):
<?php
//connect to the local MySQL
mysql_connect("localhost", "username", "password");
//select your database
mysql_select_db("flashtest");
//query the database
$query="select * from flashdata";
$result=mysql_query($query);
//find out how many entries there are
$num=mysql_num_rows($result);
//write variable out - this is what the Flash document will retreive
echo "&num=".$num;
//keep count
$count=0;
//loop through and write the data into variables for the flash document
while($row=mysql_fetch_array($result))
{
//write out each entry as a variable
$entry=$row["flash_text"];
echo "&t".$count."=".$entry;
//increment the count variable
$count++;
}
?>
Tip: you can test your PHP at this stage by fetching get_data.php in a Web browser – it should look something like this:
![]()
Now to use the data within a Flash movie, create a new Flash document and save it on the server also. The Flash document is able to access the variables written out by your PHP script via the GET variable. Open the Actions panel for Layer 1 and enter the following code (replacing ‘filepath’ with the path to your PHP script):
//load the variables into a MovieClip by calling the server-side script
var data_mc:MovieClip=_root.createEmptyMovieClip("data_mc", _root.getNextHighestDepth());
//loadVariables parameters: url, movieclip, method
loadVariables("http://localhost/filepath/get_data.php", data_mc, "GET");
//create the function for using the data
data_mc.onData=function()
{
//get the total number of entries
var numEntries:Number=data_mc.num;
//loop through the entries
for(var i:Number=0; i
Now test your movie, it should look something like this:
![]()
For Flash MX and above, you can alternatively use the LoadVars object with the following syntax:
//create LoadVars object
var lv:LoadVars=new LoadVars();
//load the data
lv.load("http://localhost/filepath/get_data.php");
//setup load function
lv.onLoad=function(success)
{
//get the data
var numEntries:Number=lv.num;
//otherwise syntax is the same as above
};
If you’re using ActionScript 3.0, the syntax will be slightly different; you need to use the URLRequest and URLLoader objects to load the data into Flash.