back to krasimirtsonev.com
to blog's home page

Load MySQL data in Flash with AS3

There are some things that I'm doing in almost every project. Loading of data in Flash is actually one of them. In this article I'll show you how to load information from MySQL database. We will use PHP as a junction between the Flash and the database. You need an Apache server like XAMPP or EasyPHP installed or just a hosting account that supports php.

Download the source files from here. Unrar the archive and upload "get.php" file on your server. Open "flash.fla" and "App.as" with Flash. Let's take a look at the action script code:

Change the value of the "URL" constant on line 13. For example if your site is "www.site.com" and you upload the "get.php" file on "www.site.com/get.php" you have to set

In line 24 I'm defining a random parameter that we will add to the URL to prevent the caching of the data. The handlers of the Loader object are set from line 28 to 32.
If you check the fla file will see that there is an empty text field on the stage. I'm using it to show the processes of our small application.
The method "output" adds text to the field. The result of the php file is accessible in "onLoadData" method, i.e. "e.target.data". In this example the php file returns only a text, but usually in real projects I'm using xml. So, when the data is loaded we send the text to our text field (line 36).

Before compiling the flash application we need a MySQL database and a table to test with. Here are several SQL commands that will create the necessary things on the server:

If you don't know how to run these commands check out here.

The "get.php" file:

You have to change the variables in lines 4, 5 and 6 depending on your server's settings.
After this little change we are ready to launch our app. Return to Flash, hold Ctrl key and press Enter. The content of the text field should be change to:
Sharing ...
Commenting ...
blog comments powered by Disqus