【发布时间】:2017-01-16 21:35:36
【问题描述】:
我正在使用 Flash AS3 开发在线游戏,并使用带有 mySQL 数据库的 PHP 服务器。我正在使用 PHP 操作 mySQL 数据库中的数据,当我在浏览器中直接从 'localhost/php/file.php' 请求 PHP 文件时,数据库会完美更改。我有以下 AS3 代码:
public function getSite(string):Boolean{
var phpVars:URLVariables = new URLVariables();
var t:Boolean = false;
/*
we use the URLRequest method to get the address of our php file and attach the php vars.
*/
var urlRequest:URLRequest = new URLRequest(string);
/*
the POST method is used here so we can use php's $_POST function in order to recieve our php variables.
*/
urlRequest.method = URLRequestMethod.POST;
/*
this attaches our php variables to the url request
*/
urlRequest.data = phpVars;
/*
we use the URLLoader class to send the request URLVariables to the php file
*/
var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
urlLoader.addEventListener(Event.COMPLETE, check(t));
t = check(t);
/*
runs the function once the php file has spoken to flash
*/
/*
we send the request to the php file
*/
urlLoader.load(urlRequest)
return t;
}
function check(t:Boolean):Function{
return function (event:Event):Boolean{
trace(event.target.data.checkResult);
if(event.target.data.checkResult == "Good"){
t = true;
} else {
t = false;
}
return t;
}
}
现在从这里开始,我的“trace”显示 URL 已加载并且输出为 "Good",但是数据库值没有改变。这是 PHP 文件:
<?php
/*
connect to our database
*/
include_once "connect.php";
$sql = "UPDATE accounts SET PlayersOnline = accounts.PlayersOnline + 1";
$query = mysql_query($sql) or exit("checkResult=Bad");
exit("checkResult=Good");
?>
当我在网络浏览器中转到'localhost/php/gameSearch.php' 时,数据库发生了变化,我想知道问题出在哪里。
【问题讨论】:
-
警告:如果您只是学习 PHP,请不要使用
mysql_query接口。它是如此可怕和危险,以至于它在 PHP 7 中被删除。像 PDO is not hard to learn 这样的替代品和像 PHP The Right Way 这样的指南解释了最佳实践。 -
另外使用
exit()打印一些东西是一种非常混乱的方式。通常你应该使用echo并让程序正常结束。
标签: php mysql actionscript-3 flash