sfmultimedia.net

sfmultimedia :: actionscript salons :: flash to PHP to MySQL roundtrip

/

Actionscript Salon #8:
Flash to PHP to MySQL Roundtrip

OK, with an up-front understanding that I'm not a PHP/MySQL guru, we're gonna take a bash this month at showing Flash interacting with these ubiquitous backend/database tools! PHP is a very popular open source server-side scripting language for backend interaction. MySQL is a powerful open source database product. Taken together (and owing to the fact that they're both free), the PHP + MySQL setup is probably the most common backend for small websites. When Flash is able to pull information from and post information to the database, the possibilities for dynamic websites and Rich Internet Applications (RIAs) really take flight. We'll learn a bit about moving information back and forth and perhaps build a simple RIA. Come ‘round and take bets on whether I'll make a fool of myself!

Relevance/availability: As robust backends become ever-more the focus of the web (think Web 2.0, collaborative authoring, etc) the information in this Salon has never been more pertinent. The techniques are shown in AS1 and are easily transferable to AS2. PLEASE PAY SPECIAL ATTENTION TO MY "NOTE FROM THE FUTURE" BELOW.

Presentation slides (swf)

Basic MySQL Query. This is as down and dirty as it gets: a very simple registration and login app designed to simply show how you can populate and read from a MySQL table.

RECIPE FOR USING THESE FILES:

  1. Make sure your website service provider offers PHP and MySQL (most do).
  2. Download the .fla and .php files.
  3. Unzip.
  4. In the PHP files, change the login, password and database names to match the settings for your MySQL database (these might be different from your FTP login information).
  5. Log in to your MySQL database. Hopefully your provider offers an admin tool...otherwise, you'll need to issue the commands from a command line interface. Check mysql.com for help on using MySQL commands.
  6. Create a table. In my code, I've named the table "basic"...if you name it something else, make sure to alter it in the PHP files! The table must have four fields with the following values:
    1. loginID (varchar with a 50 character max)
    2. nameID (varchar with a 50 character max)
    3. password (varchar with a 50 character max)
    4. timestamp (integer)
  7. FTP the PHP files to your web site. You might need to adjust your permissions for EACH file. In a command line interface (such as is available in Dreamweaver MX) you'd type SITE CHMOD 755 /path/to/phpfile.php
  8. Remember that the Flash file references the php files by name. You'll need to change the URLs to your own server to get your own tests to work.
  9. Test the swf and see if you can register. Then see if you can log in. You don't necessarily have to post the swf in order to connect to the PHP and DB. Watch the MySQL database and see if you can see the data being inserted into the DB.

Download the .php files (Zip 42k)

Download the .fla file (Zip 22k)

FlashBlog. A more complex app, demonstrating a full-scale, practical use of Flash/PHP/MySQL integration, including some thought to reasonable security measures.

NOTE FROM THE FUTURE: I wrote these materials a long time ago, and no longer consider the security measures included "reasonable". If you're following these instructions, please do some background research into MySQL injection attacks. You'll be happier in the long run.

Note that I've saved this file back to Flash MX to make it more available, but it will work fine in MX2004. The scrollpane doesn't seem to mask properly in the MX debugger, but works fine both in the MX04 debugger and when viewing in the browser.

RECIPE FOR USING THESE FILES:

  1. Make sure your website service provider offers PHP and MySQL (most do).
  2. Download the .fla and .php files.
  3. Unzip.
  4. In the PHP files, change the login, password and database names to match the settings for your MySQL database (these might be different from your FTP login information).
  5. Log in to your MySQL database. Hopefully your provider offers an admin tool...otherwise, you'll need to issue the commands from a command line interface. Check mysql.com for help on using MySQL commands.
  6. Create two tables. In my code, I've named the tables "users" and "posts" ...if you name them something else, make sure to alter those names in the PHP files!
  7. The table "users" must have six fields with the following values:
    1. userID (varchar with a 30 character max)
    2. loginID (varchar with a 30 character max)
    3. email (varchar with a 100 character max)
    4. password (varchar with a 30 character max)
    5. timestamp (integer)
    6. permission (tinyint)
  8. The table "posts" must have four fields with the following values:
    1. threadID (small integer)
    2. author (varchar with a 50 character max)
    3. body (mediumtext)
    4. timestamp (integer)
  9. You need to add one entry to the users table. This is an essential step, which will allow you to set yourself as the "master" for your own blog. You'll set your user name, login and password, but what's crucial here is that you set your permission to the number 1. Only a user with a permission of 1 can post a new thread (if you're struggling with a command line interface, the easy way to do this is to use the Flashblog app to register yourself, then go into MySQL and simply tweak the permission value from NULL to 1).
  10. FTP the PHP files to your web site. You might need to adjust your permissions for EACH file. In a command line interface (such as is available in Dreamweaver MX) you'd type SITE CHMOD 755 /path/to/phpfile.php
  11. Remember that the Flash file references the php files by name. You'll need to change the URLs to your own server to get your own tests to work.
  12. Test the swf and see if you can register. Then see if you can log in. Try posting a thread. You don't necessarily have to post the swf in order to connect to the PHP and DB. Watch the MySQL database and see if you can see the data being inserted into the DB.

Download the .php files (Zip 6k)

Download the .fla file (Zip 115k)