Aug 01 2008

AMFPHP AS3 Class and Wrapper – MySQL Query Example

Published by admin at 9:24 pm under AMFPHP, Flash AS3

Mysql to Flash via AMFPHP

Hopefully you’ve had a chance to read my other postings on AMFPHP and the classes and wrappers I’ve written for it. This post will be an example on how to query a mysql database from php and pass it into flash, then parse it into a multi-dimensional associative array (a big array that gives you the ability to loop through its records by number, and access its columns by name – similar to PHP’s mysql_fetch_assoc() ). First, if you are unfamiliar with my other postings, you can view them below:

Setting the Stage

This project requires 4 files: the php service, the two as3 classes (amfphp.as and amfWrapper.as), and the FLA. You can download them below:

Understanding the PHP Service

The service itself is fairly straight forward. First, note the important require_once line that includes the connection info to the MySQL database. This file should be basic and doesn’t necessarily have to be an external one. I keep mine external for convenience sake. Here’s the code:


$dbhost = "127.0.0.1";
$dbname = "my_database";
$dblogin = "mylogin";
$dbpass = "mypassword";

$failure = "MySQL problem. Connection to ".$dbhost." failed.";

if(!$connect = @mysql_connect($dbhost, $dblogin, $dbpass))
die($failure);
} else {
if(!@mysql_select_db($dbname,$connect)) {
die($failure);
}

Here is the class code:


require_once("connect.php");

class example {

/**
@desc - empty
*/
function example ( ){
}

/*
@desc loads the data from the mysql db, sends it to flash, which parses it
*/
function loadMysqlData($user_id) {

NetDebug::trace("Executing loadMysqlData");
NetDebug::trace("Argument user_id: ".$user_id);

$sql = sprintf("SELECT * FROM mytable WHERE id=".$user_id);
$result = mysql_query($sql);

return $result
}

}

As for the rest of the service, you have the class declaration, the constructor, and then class method called loadMysqlData, which is really the meat of the project. The NetDebug::trace() function is a debugging tool that outputs information to the AMFPHP Browser. Not really necessary, but I include it anyway out of habit. After that you have the MySQL Query statement, then the actual query when I pass that statement to the mysql_query() function that, if successful, outputs a valid resource id. Lucky for us, we can just return that resource id (as you can see from the code), and doing so passes all of the mysql info we request straight to flash (very quickly I might add; figures show amfphp is as fast as XML for most data sizes and quite a bit fast than JSON).

Inside Flash – How to handle this SQL Data

Luckily, if you are using my amfphp.as class then the function to parse out the MySQL data table is already done for you. The function is static (meaning you don’t need an instantiated class to access it) and it takes one parameter which is the object that AMFPHP gives to you – the result. The actual code for making use of this function is below:

dbData = amfphp.assocMySQL(remoteConn.serverResponse);

dbData is the final array (you must declare it as an array beforehand). For a full example of this code, simply look at the attached amfWrapper.as class. The amfWrapper class is an example implementation of the amfphp class. Thus, in the final project (fla) you only need to instantiate the wrapper in order to access the mysql information. It keeps things clean and organized.

The project files can be downloaded below:

As always, feel free to post any comments or questions. I’m always willing to help.

Popularity: 42% [?]

2 responses so far

2 Responses to “AMFPHP AS3 Class and Wrapper – MySQL Query Example”

  1. Jeromeon 17 Sep 2008 at 7:01 pm

    Where is the flash code ?
    only 1 line ? => error.

  2. kyleon 17 Sep 2008 at 11:47 pm

    download the project files for the complete source. This is just meant as an example of how to use those project files with some explanation. The
    only difference in parsing SQL is to use the one built in function, if you want that code specifically just check out the source.

Trackback URI |