How to Use Flash with PHP and mySQL
         written by ptolemy

So you want dynamic content on your Flash movie. Sounds simple, right? Well let me tell you that it is, and well, it isn't. This tutorial assumes a basic knowledge of Actionscript, PHP and MySQL. If you have that, it's time to move on. Otherwise, I recommend you do some reading on the subjects...

Setting Up your SWF
Loading external data into an SWF is a fairly simple task. Although the external text file method is probably the most widely known, it is by far the clumsiest way to get data into your SWF. Of course, this may be your only option if your server doesn't support MySQL/PHP! But for those of us who are fortunate to have these privileges, let me show you how Flash can get external data without having to create additional text files.

Dynamic text fields
In order for Flash to get data from an external source, you need to make use of dynamic text fields. Simply place a blank dynamic text field on the stage and make sure you give it a variable name (NB: dynamic text fields have both an instance AND a variable names. In this case we want to focus on the variable name). Note the name you give to the field's variable name, as we'll need it when we write our PHP script.

Once you have a text field setup, convert it to a movie clip symbol. Attach the following code to the resulting movie clip:

Ok, that's probably the easiest part of all. Now on to the PHP.

Writing the PHP script that will pass variables to your SWF
To pass a variable between PHP and you SWF, it is imperative that you know the name of the variable you want to populate within your SWF. Again, when I say variable name, I'm talking about a dynamic text field's variable, not instance, name. As long as you keep that in mind, you should be alright.

The PHP script we'll write is actually quite simple. All you need to do is use the print() function, whose output will be automatically passed to your SWF. As simple as this may seem, be warned that Flash needs to know which variable to populate with the data output from our script. In other words, we need to print the name of the variable attached to the dynamic text field we created earlier. This is a crucial step but is very simple to achieve. I've included an example below on how we would code this in PHP:

Like I said, simple concept but easy to forget.

If you've ever tried getting external data into your SWF from a text file, you'll notice that the two methods are very similar. PHP has a great advantage though in that you can use things like arrays, loops and even functions with which you can access information from within a database. The last of which will be the focus for the rest of the tutorial


  1. We have ourselves an SWF with a movie clip within which lies a dynamic text field, whose variable name is myVar.
  2. This movie clip has some code attached to it that loads a URL using the loadVariables() function.
  3. The PHP script located at the URL prints data to the screen, which is then read by the SWF and passed on to the dynamic text field with the variable name myVar.

Take a break and click the Next page link below to complete this tutorial.


Next Page





kirupa.com's fast and reliable hosting provided by Media Temple.