Flash
, PHP, and MySQL Integration
         by Ben Smith, a.k.a Ωmega: 12th May 2005

This tutorial should teach you a little bit about integration between Flash, PHP and MySQL. We will learn how to parse text into a HTML-formatted textbox from an external Actionscript file. It will use a PHP file to parse text from a MySQL database into a kind of code which the AS will decode.

As I can't use my own MySQL database here, I will provide Images showing the examples and Flash screens. Please note that to complete this tutorial, you need a PHP enabled server, a MySQL database, and Flash MX (2004).
 

[ an example of what you will create, on my site, displaying external links ]

Let's Get Started!
The following steps will show you how to create a dynamic Flash list.

  1. Create a New Document in Flash, press Ctrl+J (Cmd+J on Mac) and set the stage size to 165px by 200px (You may change the dimensions to suit your needs, but this is what I used).

  2. Create a new layer called Sites and then create a Dynamic Textbox on that layer using the Text tool and name it sites_txt. Make sure that HTML formatting is on!

[ the textbox's properties should look like this, HTML formatting is the <> button ]

  1. Now, we need to create the ActionScript file, so create a new Actionscript File by clicking File > New > Actionscript file.

  2. Now for the fun part, the coding! Insert this into the Actionscript file and save it as sites.as, the code will be explained later:

function lv(l, n, t, e, f) {
if (l == undefined) {
l = new LoadVars();
l.onLoad = function() {
var i;
n.htmlText = "";
if (t == undefined) {
n.htmlText += "<b>"+this["title"+e]+"</b><br>";
} else {
for (i=0; i<this.n; i++) {
n.htmlText += "<a href='"+this["link"+i]+"'>
"
+this["title"+i]+"</a><br>";
}
}
};
}
l.load(f);
}
lv(sites_txt, "cycle", null, "sites.php");
  1. As you can see, the Actionscript file requires a .php file (sites.php) to work, so fire up Dreamweaver/Notepad or any other text editor, and paste in this code:

<?php
mysql_pconnect ("HOST OF YOUR SQL SERVER", "YOUR SQL USERNAME", "YOUR PASSWORD");
 
mysql_select_db ("THE DATABASE YOU WANT TO USE");
$qResult = mysql_query ("SELECT * FROM sites ORDER BY id ASC");
$nRows = mysql_num_rows($qResult);
$rString ="&n=".$nRows;
for ($i=0; $i< $nRows; $i++){
$row = mysql_fetch_array($qResult);
$rString .="&id".$i."=".$row['id']."&"."&title".$i."=".$row['title']."&".
"&link".$i."=http://".$row['link']."&";
}
echo $rString."&";
 
?>
  1. As you will see if you run this file, it will not work as we need to make a table named sites in our database, so if you want to, you can use this SQL query code to create one, or create one of your own.

CREATE TABLE `sites` (
`id` int(11) NOT NULL auto_increment,
`link` varchar(100) NOT NULL default '',
`title` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=19 ;
INSERT INTO `sites` VALUES (5, 'www.kirupa.com', 'Kirupa');
INSERT INTO `sites` VALUES (4, 'www.voetsjoeba.com', 'Voetsjoeba');
INSERT INTO `sites` VALUES (3, 'www.cannedlaughter.net', 'Canned Laughter');
INSERT INTO `sites` VALUES (6, 'www.spoono.com', 'Spoono');
INSERT INTO `sites` VALUES (7, 'www.readymademag.com', 'ReadyMadeMag');
INSERT INTO `sites` VALUES (9, 'www.weebl.jolt.co.uk', 'Weebl and Bob');
INSERT INTO `sites` VALUES (10, 'www.aamukaste.org');
INSERT INTO `sites` VALUES (12, 'www.flipflopflyin.com', 'Flip Flop Flyin''');
INSERT INTO `sites` VALUES (15, 'www.kirupaforum.com', 'KirupaForum');
INSERT INTO `sites` VALUES (16, 'www.razyr.com/blog', 'Razyr');
INSERT INTO `sites` VALUES (17, 'senocular.com', 'Senocular');
INSERT INTO `sites` VALUES (18, 'www.may1reboot.com', 'May 1st Reboot');
  1. Now you are all set up, so let's include the AS file into our Flash document. Go back to your Flash timeline, create an Actions Layer, and in the first frame enter the following code:

#include "sites.as";
  1. Now you've got the basic MySQL > PHP > Flash integration going, so, publish and upload all your files to a server and test it.
Note
If this tutorial does not work, please check that you have a server that is capable of PHP and MySQL, if you do, feel free to contact me about it!

While your example should work well, I still haven't explained what all of this code does! On the next page I will explain how and why all of these sections of code work together to create a working example!


 

page 1 of 2


 

 




SUPPORTERS:

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