1

我有一个 MySQL 数据库,我通过 PHP 脚本访问它。我遇到的问题是从 PHP 返回的变量总是相同的。第一次运行代码(添加/删除数据库中的记录)时,一切都是正确的,但如果我再次运行(添加/删除)PHP 会向 AS3 报告与以前相同的记录。直接在浏览器中运行php返回是正确的。希望有人可以帮助我...我快疯了!这是我的代码:

AS3

function sendSqlData(event:Event):void
{
    playlistDateString = "&playlistDateString="+playlistDate.getFullYear().toString()+monthDigit+playlistDate.getMonth().toString()+dayDigit+playlistDate.getDate().toString();
    playlistSongNr = "&playlistSongNr="+ song;
    songTime ="&songTime="+ Math.floor(channel.position);
    var phpUrl:String = "send_data.php";
    var phpUrlRequest:URLRequest  = new URLRequest(phpUrl+parseMe+playlistDateString+playlistSongNr+songTime);

    scriptLoader = new URLLoader();
    scriptLoader.addEventListener(Event.COMPLETE, sendSqlDataComplete);
    scriptLoader.load(phpUrlRequest);
}

function getSqlData(event:Event):void
{
    var phpUrl:String = "get_data.php";
    var phpUrlRequest:URLRequest  = new URLRequest(phpUrl+parseMe);
    // phpUrlRequest.method = URLRequestMethod.POST;

    scriptLoader = new URLLoader();
    // scriptLoader.dataFormat = URLLoaderDataFormat.TEXT;
    scriptLoader.addEventListener(Event.COMPLETE, getSqlDataComplete);
    scriptLoader.load(phpUrlRequest);
}
function sendSqlDataComplete(event:Event):void
{
    scriptLoader.removeEventListener(Event.COMPLETE, sendSqlDataComplete);
    var phpVars:URLVariables = new URLVariables();
    phpVars.decode(event.target.data);

    getSqlData(event);
}
function getSqlDataComplete(event:Event):void
{
    scriptLoader.removeEventListener(Event.COMPLETE, getSqlDataComplete);
    var phpVars:URLVariables = new URLVariables();
    phpVars.decode(event.target.data);

    var phpString = event.target.data.toString();

    var patterns:Array = ["id\\d{0,5}=\\d{0,5}", "playlistDate\\d{0,5}=\\d{0,8}", "playlistSongNr\\d{0,5}=\\d", "songTime\\d{0,5}=\\d{0,7}"];

    for (var j:int = 0; j < 4; j++)
    {
        var pattern:RegExp = new RegExp(patterns[j],"g");
        var tempStrings:Array = phpString.match(pattern);

        for (i = 0; i < Number(phpVars.records); i++)
        {
            switch (j)
            {
                case 0:
                    ids[i] = Number(tempStrings[i].substring( tempStrings[i].indexOf("=")+1 ));
                break;

                case 1:
                    playlistDates[i] = Number(tempStrings[i].substring( tempStrings[i].indexOf("=")+1 ));
                break;

                case 2:
                    playlistSongNrs[i] = Number(tempStrings[i].substring( tempStrings[i].indexOf("=")+1 ));
                break;

                case 3:
                    songTimes[i] = Number(tempStrings[i].substring( tempStrings[i].indexOf("=")+1 ));
                break;
            }
        }
        tempStrings = null;
    }

    fillDataGrid(Number(phpVars.records));
}

PHP:发送数据.php

<?php
include_once ('connect.php');

$playlistDateString = trim($_GET['playlistDateString']);
$playlistSongNr = trim($_GET['playlistSongNr']);
$songTime = trim($_GET['songTime']);

if($connection)
{
    $status .= ("connect=ok&");

    //Select database
    mysql_select_db($dbname, $connection);

    $sql="INSERT INTO $tablename1 (ID, playlistDate, playlistSongNr, songTime, comment) VALUES ('', '$playlistDateString', '$playlistSongNr', '$songTime', 'comment');";
    $status .= ("sql=".$sql."&");

    // Execute query
    if (mysql_query($sql,$connection) )
    {
        $status .= ("query=ok");
    }
    else
    {
        $status .= ("query=error:".mysql_error());
    }
}
else
{
    $status = ("connect=error: ".mysql_error());
}

echo $status;
mysql_close($connection);
?>

PHP get_data.php

<?php

include_once ('connect.php');

if($connection)
{
    $status .= ("connect=ok");
    //Select database
    mysql_select_db($dbname, $connection);
    //Execute query
    $query = mysql_query("SELECT * FROM $tablename1");

    if ($query)
    {
        $result = "records=".(mysql_num_rows($query));
        $i = 0;
        while ($row = mysql_fetch_array($query))
        {
            $result .= "&id".$i."=".($row["ID"]);
            $result .= "&playlistDate".$i."=".($row["playlistDate"]);
            $result .= "&playlistSongNr".$i."=".($row["playlistSongNr"]);
            $result .= "&songTime".$i."=".($row["songTime"]);
            $result .= "&comment".$i."='".($row["comment"])."'";
            $i++;
        }
        $status .= ("&receive=ok");
        echo $result."&";
    }
    else
    {
        $status .= ("&receive=error");
    }
}
else
{
    $status .= ("connect=error:".mysql_error());
}
echo $status;

mysql_close($connection);
?>
4

1 回答 1

0

当您从 ActionScript 调用它时,您的响应似乎正在被缓存。您可以在 URL 的末尾添加一个微时间值(这将防止缓存)。

var phpUrl:String = "get_data.php?" + new Date().getTime();
于 2013-10-15T10:53:20.400 回答