Rich Internet Applications for SLA Research/Basic Database Interaction

As discussed in the Overview, the AMFPHP scripts allow Flash movies and Flash Communication Server applications to communicate with PHP scripts. These scripts can receive, process, and return data to the requesting movie or application. In the context of SLA research, perhaps the most useful function of such scripts is database interaction involving the inserting, updating, and retrieving of information.

Setting Up AMFPHP
After downloading the AMFPHP scripts from http://www.amfphp.org and unzipping them, the gateway.php file should be placed in a directory of the web server; be sure that it is a directory in which the execution of scripts is allowed by the web server. On many servers, this is within the cgi-bin directory or a subdirectory thereof. The AMFPHP flashservices folder is typically installed within the cgi-bin directory. Then, a subfolder for access by the individual Flash movie (or FlashComm application) should be created. Into this subfolder should be placed gateway.php; normally, this file should not require any editing. For this tutorial, the subfolder will be named mysql_amfphp.

Finally, a subfolder named services should be created in the mysql_amfphp folder. The services folder will contain the PHP file that accesses the database. For this sample application, a file named MySQLQuery.php should be created and the following code should be placed within it using a basic text editor (Notepad on Windows, vi or emacs on UNIX/LINUX, for example).

methodTable = array(      "inRecord" => array( "description" => "Inserts the passed argument into the database", "access" => "remote", // available values are private, public, remote "arguments" => array ("arg1", "arg2") ),      "outRecord" => array( "description" => "Echos the passed argument back to Flash (no need to set the return type)", "access" => "remote", // available values are private, public, remote "arguments" => array ("arg1") )    );   }   function inRecord($fName, $lName){ $dbc = mysql_connect("DBNAME", "USERID", "PASSWORD"); mysql_select_db("amfphp_test"); $sql = "INSERT INTO people ( UID, firstName , lastName ) " . "VALUES ( '', '$fName', '$lName' )"; $result = @mysql_query ($sql); // run the query return " *-* ". $lName. ", " . $fName. " *-* (" . $result . ")"; }  function outRecord($message){ $dbc = mysql_connect("DBNAME", "USERID", "PASSWORD"); mysql_select_db("amfphp_test"); $result = mysql_query('SELECT * FROM people'); while ($row = mysql_fetch_array($result)) { $nameList .= $row['lastName']. ", " . $row['firstName']. "; ";    }     return $message. " ^**mysql_amfphp**^ ". "\n". $nameList. "\n"; }  } ?>

Setting Up the Database
In the example code, the values DBNAME, USERID, and PASSWORD should be changed to reflect the specific database server to be accessed. Furthermore, the database should contain a database named amfphp_test, which contains a table named people. The table people should, in turn, contain two columns: firstName and lastName.

Creating the Flash Movie
Using the components window in Flash MX 2004, create a user interface with the elements shown in the image.

The UI elements above should be given the following names:
 * top text area: 'show_txt'
 * list records button: callButton
 * clear textfield button: clearButton
 * firstName text line: inFirstName
 * lastName text line: inLastName
 * insert record button: insertButton

These names will match the following ActionScript code, which should be placed in the first frame of the main timeline of the Flash movie:

// Necessary class (needed for AMFPHP) // just for debugging purposes // Create the AMFPHP gateway and connect to the server. // The gateway.php file points to the services folder (see below, // where the service is actually called with getService("MySQLQuery") NetServices.setDefaultGatewayUrl("http://www.MYSERVERNAME.com/cgi-bin/mysql_amfphp/gateway.php"); conn = NetServices.createGatewayConnection; //Result handler request = new Object; request.onResult = function(result) {   show_txt.text = result; } // Call the service; name of service (MySQLQuery) must be the same // as the pre-extension name of the file in the "services" folder: "MySQLQuery.php" service = conn.getService("MySQLQuery"); // Buttons  callButton.onPress = function{   service.outRecord(request, show_txt.text);   show_txt.text = ""; } clearButton.onPress = function{   show_txt.text = ""; } insertButton.onPress = function  {   service.inRecord(request, inFirstName.text, inLastName.text); }; stop;
 * 1) include "NetServices.as"
 * 1) include "NetDebug.as"

After exporting the movie, a user should be able to insert first- and last-name pairs into the database. The PHP code returns a confirmation message both when inserting and retrieving database information. When inserting information, the first and last names will be returned; when retrieving, whatever text is in the upper window will be sent and added to the beginning of the returned information, followed by a list of the names (in last name, first name order) in the database.

= Possible Uses = The sample application above illustrates how a Flash movie might access a database. Such access might be used for login authentication, creating a new user account, or saving textual information for future retrieval and study.

It is worth noting that information in a database needn't be accessed exclusively by the AMFPHP code or Flash movie that created it. If, for example, the transcript of a text chat were saved as a database entry, another Flash movie could be used to retrieve it for review by a researcher. A PHP script, likewise, could retrieve the transcript and perform text manipulation operations on it (e.g., placing the HTML tags for bold text,  & , around words not in the 1000 most frequent words/word families in English).

In the next section, a sample application will be used to demonstrate how text from a chat application might be saved for later retrieval.