Flash从MYSQL中检索JSON结果

时间:2014-07-16 15:33:34

标签: php mysql

我的应用程序通过Flash调用以获取国家/地区列表,但在我的PHP中,结果返回空,我使用phpMyAdmin创建了表,并且我有两行。

以下是代码Action脚本3:

package  {
import flash.net.URLRequest;
import flash.net.URLVariables;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.events.EventDispatcher;

public class SQL extends EventDispatcher{

    var url:String = "";
    var urlRequest:URLRequest;

    public function SQL() {
        // constructor code
    }

    public function Post(url:String, urlVaribles:URLVariables = null):void{

        this.url = url;
        this.urlRequest = this.urlRequestObj();
        var loader:URLLoader = new URLLoader();
        if(urlVaribles){ 
            this.urlRequest.data = urlVaribles;
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        }

        loader.addEventListener(Event.COMPLETE, dataPostOnLoad);
        loader.load(this.urlRequest);
    }

    public function Get(url:String, urlVaribles:URLVariables = null):void{

        this.url = url;
        this.urlRequest = this.urlRequestObj();
        var loader:URLLoader = new URLLoader();
        if(urlVaribles){ 
            this.urlRequest.data = urlVaribles;
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
        }

        loader.addEventListener(Event.COMPLETE, dataGetOnLoad);
        loader.load(this.urlRequest);
    }

    private function urlRequestObj():URLRequest{
        return new URLRequest(this.url);
    }

    private function dataPostOnLoad(evt:Event):void{
        var evt2:SQLEvent=new SQLEvent(SQLEvent.POST_COMPLETE, evt.target.data);
        dispatchEvent(evt2);

    }

    private function dataGetOnLoad(evt:Event):void{
        trace("IN GET " + evt.target.data);
        var evt2:SQLEvent=new SQLEvent(SQLEvent.GET_COMPLETE, evt.target.data);
        dispatchEvent(evt2);

    }
}

}

来自Flash的来电代码:

import fl.motion.MotionEvent;

var sql:SQL = new SQL();
sql.addEventListener(SQLEvent.GET_COMPLETE, dataGetResponse);
sql.Get("http://localhost:8888/MAMP/HUGT/getCountriesDP.php");

mc_ddScroll.visible = false;
mc_ddScrollButton.addEventListener(MouseEvent.CLICK, clickScrollButton);

function dataGetResponse(e:SQLEvent):void {
trace("Countries " + e.params);
}

function clickScrollButton(e:MouseEvent):void{

if(mc_ddScroll.visible){
    mc_ddScroll.visible = false;
}
else{

mc_ddScroll.visible = true;
}

}

最后是PHP脚本:

getCountriesDP.php

<?php

include "connect.php";

$result = mysql_query($conn,"SELECT * FROM C_Countries");

if(mysql_num_rows($result)){
    echo '{"countries":[';

    $first = true;
    $row=mysql_fetch_assoc($result);
    while($row=mysql_fetch_row($result)){
    //  cast results to specific data types

    if($first) {
        $first = false;
    } else {
        echo ',';
    }
    echo json_encode($row);
    }
    echo ']}';
} else {

    echo "[]";
}

mysqli_close($conn);



?>

Connect.php

<?php 


$conn = mysql_connect("localhost","root","root");
mysql_select_db("HUGT", $conn);

// disable reporting errors for security reason
    error_reporting(0);

    // Error checking
    if(!$conn) {
        die('Could not connect ' . mysql_error());
    }





?>

1 个答案:

答案 0 :(得分:0)

我刚刚将我的php脚本更改为:

<?php 


$conn = mysqli_connect("localhost","root","root", "HUGT");
//mysql_select_db("HUGT", $conn);

// disable reporting errors for security reason
    error_reporting(0);

    // Error checking
    if(mysqli_connect_errno()) {
        die('Could not connect ' . mysqli_connect_error());
    }





?>

<?php

include "connect.php";

$result = mysqli_query($conn,"SELECT * FROM C_Countries");


$rows = array();
while($r = mysqli_fetch_assoc($result)) {
    $rows[] = $r;
}

echo json_encode($rows);

mysqli_close($conn);



?>