天天看點

Amfphp與Flex互動:讀取mysql資料

伺服器端代碼,amfphp的services目錄下建立readData.php檔案,代碼如下

<?php

    class ReadDB

    {

              function getData()

             {

                  mysql_connect("localhost","root","root");

                  mysql_select_db("mmmm");

                  mysql_query("SET NAMES UTF8");

                  $sql="select * from mmmm";

                  return mysql_query($sql);

             }

    }

?>

flex代碼

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF">

    <mx:RemoteObject id="myservice" fault="FaultHandler(event)" showBusyCursor="true" source="ReadDB" destination="amfphp">

        <mx:method name="getData" result="ReadHandler(event)" />

    </mx:RemoteObject>

<mx:Script>

        <![CDATA[

            import mx.managers.CursorManager;

            import mx.rpc.events.ResultEvent;

            import mx.rpc.events.FaultEvent;

            //注:錯誤處理函數

            private function FaultHandler(fault:FaultEvent):void

            {

                 //注:删除忙狀态光标             

                 CursorManager.removeBusyCursor();

                 //注:傳回的錯誤資訊,讓其在result_test中顯示出來

                 result_text.text = "code:/n" + fault.fault.faultCode + "/n/nMessage:/n" + fault.fault.faultString + "/n/nDetail:/n" + fault.fault.faultDetail;

            }

            private function ResultHandler(evt:ResultEvent):void

            {

                 //注: 将獲得的結果轉化為String類型,并交給result_text顯示出來

                 result_text.text = evt.message.body.toString(); 

            }

            private function ReadHandler(evt:ResultEvent):void 

            {

                 dg.dataProvider = evt.result;

            }                

        ]]>

    </mx:Script>

    <mx:TextArea x="10" y="36" width="319" height="113" id="result_text"/>

    <mx:Label x="10" y="10" text="Result:"/> 

    <mx:DataGrid x="337" y="36" width="821" id="dg" height="319">

        <mx:columns>

            <mx:DataGridColumn headerText="lastname" dataField="lastName"/>

            <mx:DataGridColumn headerText="firstname" dataField="firstName"/>

            <mx:DataGridColumn headerText="phone" dataField="phone"/>

            <mx:DataGridColumn headerText="email" dataField="email"/>

        </mx:columns>

    </mx:DataGrid>

    <mx:Button x="1058" y="363" label="view" click="myservice.getOperation('getData').send();" width="100"/>

</mx:Application>

繼續閱讀