matlabmann 2016-09-08 20:26
浏览 41
已采纳

在Flash AS3中加载PHP URL

I am working on an online game in Flash AS3 and utilizing a PHP server with mySQL database. I am manipulating the data in mySQL database using PHP and when I request the PHP file in a browser straightly from 'localhost/php/file.php', the database changes perfectly. I have the following AS3 code:

    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;
    }
}

Now from here, my "trace" shows that the URL is loaded and the output is "Good", however the database values does not change. This is the PHP file:

   <?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");
   ?>

When I go to 'localhost/php/gameSearch.php' in my web browser the database changes, and I am wondering what the problem is.

  • 写回答

1条回答 默认 最新

  • dopcpc9207 2016-09-09 08:32
    关注

    You have a "caching" problem. In other words, the result of the already requested URL is cached to reduce latency and access times, and what you've represented is the cached copy of the output and not a fresh output resulting from the execution of the instructions on behalf of the server.

    To overcome the issue, you could've pushed a no-cache header to the requestHeaders property on your "request" object (the property is of type URLRequestHeader). However, the runtime looks to be ignorant on the header and it always provides the cached copy!

    To overcome that issue, however, you need to fool the runtime as if you are requesting a new URL every time by appending a dummy random-valued variable:

    getSite("localhost/php/file.php?r="+Math.random());
    


    And regarding your specific provided code; The URLLoader works asynchronously, that's why you register a "on complete" listener! The t = check(t); statement induces you're attempting to "check" the result while it may not be ready by then! You should check it when/after the listener triggered. In addition to the fact that the assignment is syntactically inappropriate (assigning a Function to a Boolean!) and reconsider the logic of the check function!

    And in the PHP code, as others have suggested, ultimatly don't use the deprecated mysql_query function and use a more appropriate API.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器