在 Flash AS3 中加载 PHP URL
Loading PHP URL in Flash AS3
我正在 Flash AS3 中开发在线游戏,并使用 PHP 服务器和 mySQL 数据库。我正在使用 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'
时,数据库发生了变化,我想知道问题出在哪里。
您有一个 "caching" 问题。换句话说,已请求 URL 的结果被缓存以减少延迟和访问时间,而您所代表的是输出的缓存副本而不是 fresh 代表服务器执行指令的结果。
要解决此问题,您可以将 no-cache
header 推送到 "request" object 上的 requestHeaders
属性 (属性 的类型是 URLRequestHeader
)。但是,运行时看起来对 header 一无所知,它总是提供缓存副本!
然而,为了克服这个问题,您需要通过附加一个虚拟 random-valued 变量来欺骗运行时,就好像您每次都在请求一个新的 URL:
getSite("localhost/php/file.php?r="+Math.random());
关于您提供的特定代码; URLLoader
异步工作,这就是为什么你要注册一个 "on complete" 监听器! t = check(t);
语句会导致您尝试 "check" 结果,而到那时它可能还没有准备好!您应该检查 when/after 侦听器是否触发了它。除了赋值在语法上不合适(将一个Function
赋值给一个Boolean
!)并重新考虑check
函数的逻辑!
并且在 PHP 代码中,正如其他人所建议的那样,最终不要使用已弃用的 mysql_query
函数并使用更合适的 API.
我正在 Flash AS3 中开发在线游戏,并使用 PHP 服务器和 mySQL 数据库。我正在使用 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'
时,数据库发生了变化,我想知道问题出在哪里。
您有一个 "caching" 问题。换句话说,已请求 URL 的结果被缓存以减少延迟和访问时间,而您所代表的是输出的缓存副本而不是 fresh 代表服务器执行指令的结果。
要解决此问题,您可以将 no-cache
header 推送到 "request" object 上的 requestHeaders
属性 (属性 的类型是 URLRequestHeader
)。但是,运行时看起来对 header 一无所知,它总是提供缓存副本!
然而,为了克服这个问题,您需要通过附加一个虚拟 random-valued 变量来欺骗运行时,就好像您每次都在请求一个新的 URL:
getSite("localhost/php/file.php?r="+Math.random());
关于您提供的特定代码;
URLLoader
异步工作,这就是为什么你要注册一个 "on complete" 监听器! t = check(t);
语句会导致您尝试 "check" 结果,而到那时它可能还没有准备好!您应该检查 when/after 侦听器是否触发了它。除了赋值在语法上不合适(将一个Function
赋值给一个Boolean
!)并重新考虑check
函数的逻辑!
并且在 PHP 代码中,正如其他人所建议的那样,最终不要使用已弃用的 mysql_query
函数并使用更合适的 API.