从PHP检索ActionScript并返回null值

时间:2012-01-08 08:01:39

标签: actionscript

我正在尝试通过php脚本从数据库中检索数据,并使用actionscript 3在flash中显示。

对于动作3,我有两个功能:

private var postArrayTxt:Array;

        public function stampTwo() {
            // constructor code

            var stampNumber1:MovieClip = new stamp1();
            var stampNumber2:MovieClip = new stamp2();
            var stampNumber3:MovieClip = new stamp3();

            postArrayTxt = new Array();
            postArrayTxt[0] = stampNumber1;
            postArrayTxt[1] = stampNumber2;
            postArrayTxt[2] = stampNumber3;
            trace("All stamps works");

            retrieveDetailsFromDB();

        }

从数据库中检索的数据将显示在将调用retrieveDetailsFromDB()的各种动画片段中。

public function retrieveDetailsFromDB():void {

            var myLoader:URLLoader = new URLLoader();
            myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
            myLoader.load(new URLRequest("http://localhost/Converse/stampGalore/tryout.php"));
            myLoader.addEventListener(Event.COMPLETE, onDataLoad);

            // Error Handling
            myLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            myLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);

            // Could be an error or just a message
            myLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);

            function onDataLoad(evt:Event): void {
                //var loader:Loader = new Loader();
                //stamp221.addChild(loader);
                //loader.load(new URLRequest(evt.target.data.facebookRemarks));
                var delimiter:String = "|^_^|";
                var stamp:String = evt.target.data.databaseRemarks;
                trace(stamp);

                var stampRemarkArr:Array = new Array();
                stampRemarkArr = stamp.split(delimiter);


                for (var i:Number=0; i<stampRemarkArr.length; i++) {
                    postArrayTxt[i].text = String(stampRemarkArr[i]);
                    trace("ended");
                } 
            }

            // error callbacks

            function onIOError(evt:IOErrorEvent) {
                trace("IOError: " + evt.text);
            }

            function onHTTPStatus(evt:HTTPStatusEvent) {
                trace("HTTPStatus: " + evt.status);
            }

            function onSecurityError(evt:SecurityErrorEvent) {
                trace("SecurityError: " + evt.text);
            }
        }

最后但并非最不重要的是,这是我的PHP脚本。

<?php
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
include_once "mysqli.connect.php";


$sql = "SELECT remarks FROM stamp";
$result = $mysqli->query($sql);
if ($mysqli->errno)
{
    error_log($mysqli->error);
    return;
}


$facebook = "";
$counter = 0;

while ($row = $result->fetch_array())
{
    $database = $row["remarks"];
    $delimiter = "|^_^|";

    if ($counter == 0) {
        $facebook .=$database;
    } else {
        //Use a delimiter "|^_^|" to seperate the records
        $facebook .= $delimiter . $database;
    }

    $counter++;
}
$mysqli->close();

    echo "databaseRemarks=" . $facebook;
?>

如果我自己运行php脚本,可以从数据库中检索数据。但是,如果我在Flash中运行,它将返回一个空值。请帮助我,因为我浪费了很多时间在这个检索功能上。谢谢

1 个答案:

答案 0 :(得分:0)

这不会起作用:

var stamp:String = evt.target.data.databaseRemarks;

Flash并不知道您的数据采用哪种格式,因此您需要先解析它。所以首先阅读数据:

var rawData:String = evt.currentTarget.data;

然后解析它:

var parsedData:* = someFunctionToParseYourData(rawData);