用於SLA研究的富網際網路應用/基本資料庫互動
如概述中所述,AMFPHP 指令碼允許 Flash 動畫和 Flash Communication Server 應用程式與 PHP 指令碼通訊。這些指令碼可以接收、處理並返回資料到請求的動畫或應用程式。在 SLA 研究的背景下,也許這類指令碼最有用的功能是涉及插入、更新和檢索資訊的資料庫互動。
從http://www.amfphp.org下載 AMFPHP 指令碼並解壓縮後,應將gateway.php檔案放置在 Web 伺服器的目錄中;確保它是一個 Web 伺服器允許執行指令碼的目錄。在許多伺服器上,這在cgi-bin目錄或其子目錄中。AMFPHP 的flashservices資料夾通常安裝在cgi-bin目錄中。然後,應為單個 Flash 動畫(或 FlashComm 應用程式)建立用於訪問的子資料夾。應將gateway.php放置到此子資料夾中;通常,此檔案不需要任何編輯。對於本教程,子資料夾將命名為mysql_amfphp。
最後,應在mysql_amfphp資料夾中建立名為services的子資料夾。services資料夾將包含訪問資料庫的 PHP 檔案。對於此示例應用程式,應建立一個名為MySQLQuery.php的檔案,並將以下程式碼放置在其中,使用基本的文字編輯器(例如,Windows 上的記事本,UNIX/LINUX 上的 vi 或 emacs)。
<?php
class MySQLQuery{
function MySQLQuery(){
$this->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";
}
}
?>
在示例程式碼中,DBNAME、USERID和PASSWORD的值應更改為反映要訪問的特定資料庫伺服器。此外,資料庫應包含名為amfphp_test的資料庫,該資料庫包含名為people的表。表people又應包含兩列:firstName和lastName。
使用 Flash MX 2004 中的元件視窗,建立具有影像中所示元素的使用者介面。
以上 UI 元素應使用以下名稱:
- 頂部文字區域:'show_txt'
- 列出記錄按鈕:callButton
- 清除文字欄位按鈕:clearButton
- firstName文字行:inFirstName
- lastName文字行:inLastName
- 插入記錄按鈕:insertButton
這些名稱將與以下 ActionScript 程式碼匹配,這些程式碼應放置在 Flash 動畫主時間軸的第一幀中
// Necessary class (needed for AMFPHP)
#include "NetServices.as"
// just for debugging purposes
#include "NetDebug.as"
// 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();
匯出動畫後,使用者應該能夠將第一姓名和最後姓名對插入資料庫。PHP 程式碼在插入和檢索資料庫資訊時都會返回確認訊息。插入資訊時,將返回第一姓名和最後姓名;檢索時,將傳送上視窗中的任何文字並將其新增到返回資訊的開頭,然後是資料庫中姓名的列表(以姓氏,名字的順序)。
以上示例應用程式說明了 Flash 動畫如何訪問資料庫。這種訪問可能用於登入身份驗證、建立新使用者帳戶或儲存文字資訊以備將來檢索和研究。
值得注意的是,資料庫中的資訊不必專門由建立它的 AMFPHP 程式碼或 Flash 動畫訪問。例如,如果文字聊天的記錄被儲存為資料庫條目,則另一個 Flash 動畫可以用於檢索它以供研究人員檢視。同樣,PHP 指令碼可以檢索記錄並對其執行文字操作(例如,在不在英語中最常見的 1000 個單詞/詞族中的單詞周圍放置粗體文字的 HTML 標籤,<b> & </b>)。
在下一節中,將使用示例應用程式來演示如何儲存來自聊天應用程式的文字以供以後檢索。