Use JSON to connect Flash to a Database

I’ve worked through a few samples of JSON AS3 code so that I can hopefully create a simple example that readers may take away and expand for their needs. In a nutshell, I’ve used PHP to encode content into JSON which can then be decoded in AS3 to allow you to query a database in AS3 and get your results in Flash. I’ve shown all my PHP code in this post, however for the AS3 code download my zip file and run it in Flash. Note: I created this using CS4. I’m trying not to get out of this easy, but I feel the best way to understand the code is just download it, and look at how it works. Any questions, leave a comment!

Why add a database to Flash

Because you don’t want static content! By adding a database to your Flash files you allow for all sorts a possibilities. You can query a database, allow users to filter their data, add data to the database, and just make your page much more interesting!

Use PHP To Make the Database Connection

I use ADOdb Lite to make sure I have a secure database connection. You can check it out at the ADOdb sourceforge site. I’ll include the ADOdb files with the source code at the end of this post.

Get data from your database to your Flash file

I’ve created a file called query.php which acts as an intermediary between the database and Flash. query.php will receive a $_GET value named “name” which it will use to query the database. The first two are sample cases in the switch statement are just there to test that the query.php file is working. So if you go to query.php?name=car you should some output and likewise for query.php?name=truck.


< ?php
require_once 'config.php';
$name = $_GET['name'];
switch($name)
{
case "car":
$pp -> vehicle_make = "Ford";
$pp -> vehicle_model = "Mustang";
$pp -> vehicle_year = "2009";
$arr = array($pp);
echo json_encode($arr);
break;
case "truck":
$pp -> vehicle_make = "Ford";
$pp -> vehicle_model = "F-150";
$pp -> vehicle_year = "2000";
$arr = array($pp);
echo json_encode($arr);
break;
case "get all":
$dbResult = $qdb->Execute("SELECT * FROM work");
$results = $dbResult->GetAll();
$strippedResults = array();
foreach($results as &$result)
{
$strippedResults []= array(
'title' => stripcslashes($result['title']),
'description' => stripcslashes($result['description']),
'image' => stripcslashes($result['image'])
);
}
print_r(json_encode($strippedResults));
break;
default:
$pp -> results = "no valid information";
$arr = array($pp);
echo json_encode($arr);
break;
}
?>

Moving onto the AS3 Code

First thing’s first, make sure you have your required files to make sure Flash and AS3 can make sense of the JSON code.

import com.adobe.serialization.json.JSON;

I’ve zipped up a sql dump and all of the files for this project into a folder. Unzip the files, start up MAMP or the server of your choice, add the database, make sure the config.php file is configured correctly and you should be in action! Holler at me if you have any questions. If this post is popular I may go a bit more in depth into how to use this method of accessing a database to add data to a site and create a few examples.

Zipped Files here
final result


This entry was posted on Monday, October 19th, 2009 at 6:43 pm and is filed under Programming, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

One Response to “Use JSON to connect Flash to a Database”

  1.     php query | PHP Says:

        October 30th, 2009 at 9:34 am

    […] propstm.net » Blog Archive » Use JSON to connect Flash to a Database […]