Building Data-Driven Flash Movies
Hook your Flash movie clips up to a database.
| No Hands |
Obviously, that was a very primitive example, though one that did demonstrate the concept of integrating data from an external source with a Flash movie. Now, how about a twist?
Let's suppose I modify the Flash file above so that the call to loadVariables() loads variables from a server-side script ("message.php"), rather than a static text file.
loadVariablesNum ("http://localhost/message.php", 0);
This "message.php" script contains code to retrieve a message (either from a database or other data source) and output it in the format Flash needs. Here's what the script looks like:
// open connection to database
$connection = mysql_connect("localhost", "root", "secret") or die ("Unable to connect!");
mysql_select_db("data") or die ("Unable to select database!");
// formulate and execute query
$query = "SELECT message FROM message_table";
$result = mysql_query($query) or die("Error in query: " . mysql_error());
// get row
$row = mysql_fetch_object($result);
// print output as form-encoded data
echo "msg=" . urlencode($row->FirstName);
// close connection
This should be fairly simple to understand, even if you've never programmed in PHP before. Very simply, I'm opening a connection to a MySQL database, running an SQL query to retrieve the contents of the "message" table, and displaying that content as form-encoded data.
Here's an example of what this script might output:
''.preg_replace(array('/ /', '/ /'), array(' ', ' '), '
Now, when you run the Flash movie, it will access the specified URL, retrieve the form-encoded variable-value pair, parse it, and insert the variable values into the movie. Which is why the Flash movie will look like this:
Any change to the database record will be immediately reflected in the Flash file - with zero changes required to the Flash source.
In case you don't want to use a database as your data source, you can even run a shell command and send the output of the command to the Flash movie for display. Consider this variant of the script above, which displays the output of the "fortune" program (a new message every time) whenever the Flash movie is played back in your browser.
$output = `/usr/games/fortune`;
echo "msg=" . urlencode($output);
How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.
Read more, or grab your copy now!