# 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).

## 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:
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, <b> & </b>, 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.