如何使用.php将我的得分值(在flash/actionscript 3中)发送到数据库


how to send my score value (in flash/actionscript 3) to database by using .php

我有一个flash游戏。我想通过.php将我的分数发送到数据库。我该怎么做?我写了一些帖子,但我不知道怎么写。我应该在actionscript 3端和php端做什么?我的数据库名称是mydb,变量是score。

首先需要创建一个URLLoader,并将要发送的变量存储到请求的头中。加载URL并发送变量后,我们将侦听COMPLETE事件,以便将变量从PHP返回到Flash。

ActionScript 3代码:

private function SendScore(score:int) {
    //Use URLVariables Class to store our Variables and send them to our URL
    var variables:URLVariables = new URLVariables();
    //you can create as many variables you want (variables.variablename);
    variables.score = score;
    //URLLoader to load the URL
    var urlloader:URLLoader = new URLLoader;
    //Simple URLRequest with our URL
    var urlrequest:URLRequest = new URLRequest('http://www.mysite.com');
    //We set the method to POST. You can also use GET
    urlrequest.method = URLRequestMethod.POST;
    //We declare our set of variables to the data of our urlrequest
    urlrequest.data = variables;
    //We load the URL           
    urlloader.load(urlrequest);
    //We need to listen to an Event.COMLETE when we want to load variables FROM PHP
    urlloader.addEventListener(Event.COMPLETE, CompleteHandler, false, 0, true);
    //With  the listening to IOErrorEvent.IO_ERRORwe can intercept an error and can use it for Debugging
    urlloader.addEventListener(IOErrorEvent.IO_ERROR , ErrorHandler, false, 0, true);
}
//CompleteHandler will be used when the Load of the URL is completed
private function CompleteHandler(e:Event) {
    //Received Variables from PHP script
    var vars:URLVariables = new URLVariables(e.target.data);
    //You can access all variables from PHP with vars.xxxx
    //Example: vars.var1, vars.var2
    if(vars.success) trace('Saving succeeded');
    else ('Saving failed');
}
//ErrorHandler to receive error messages and don't fire exception errors
private function ErrorHandler(e:IOErrorEvent) {
    trace('Error occured');
}

在PHP中,我们使用方法POST从Flash接收变量,并可以将它们与$_POST['xxx']一起使用。我们将分数插入数据库,检查insert是否成功,并将此变量发送回"浏览器"/Flash。

PHP代码(我使用PDO进行数据库操作:http://php.net/manual/de/book.pdo.php):

try {
    //Establishing database connection with PDO
    $DB = new PDO($dsn, $user, $pw, array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
} catch (PDOException $e) {
    //Error catching
    print "Connection to Database failed: " . $e->getMessage() . "<br />";
    die();
}
//Create a SQL statement, prepare it and execute it to INSERT the data into database
//the "variables.score" from AS3 can be read with $_POST['score'], cause we used POST as method
$sql = "INSERT INTO my_db (is_score) VALUES ('".$_POST['score']."');";
$stmt = $DB->prepare($sql);
$stmt->execute();
//Here we send back a variable to AS3:
//If you want to send more variables back use the & sign to append more variables
//example: 'succeds=true&var1=this&var2=there'
//Can be read in CompleteHandler with vars.success
if($stmt->rowCount() > 0) echo('success=true');
else echo('success=false');

最简单的方法是从flash应用程序中调用php-web脚本,使用HTTPGET将新分数发送到URL。使用URLLoader。然后,PHP脚本可以向Flash应用程序返回一个确认,即一切正常。

这里有一个关于如何使用URLLoader的例子,简单的PHP知识就足够了。