4 Replies Latest reply on Apr 24, 2009 5:42 AM by snowscreen

    Help with retrieving data from SQL with server side PHP-Script (Async Token)

    snowscreen
      May I dare to ask a question?
      I've established a connection in FLEX Builder 3
      to a MySQL database thorugh a communication with an
      automatic generated php script.
      But when I change the AS3 script, which communicates
      with the php script a problem occures.
      The data just gets lost through the process!

      Can anyone please tell me why, or correct the script beneath?


      //////////////////////////////////////////////////////////////////////////////
      private function getThis(searchString:String):Object
      {

          /**
           * find the order parameters
           */

           var temp:Object = new Object();
          var desc:Boolean = false;
          var orderField:String = '';   

          
      var uidCol:String;

          var parameters:* =
          {
              "orderField": orderField,
              "orderDirection": (desc) ? "DESC" : "ASC",
              "filter": searchString
          }
           /**
            * execute the server "select" command
            */

          searchRequest("FindAll", parameters);
        
       
               function searchRequest(method_name:String, parameters:Object):void
                {
                  // add the method to the parameters list
               parameters['method'] = method_name;
          
               gateway.request = parameters;
          
               var call:AsyncToken = gateway.send();
               call.request_params = gateway.request;
               call.handler = searchReqHandler
             
             
                    function searchReqHandler(e:Object):void
                    {
                         if (e.isError)
                         {
                             Alert.show("Error: " + e.data.error);
                         }
                         else
                         {        
                             i++;
                             for each(var row:XML in e.data.row)
                             {
                                 temp = new Object();
                                 for (var key:String in fields) //fields = row names
                                 {
                                     temp[key + 'Col'] = row[key];
                                 }                        

                                 uidCol = temp.uidCol;
                                 trace(uidCol); // // Gives>"656884" - the right answer from db                 
                             }                                          
                             trace(uidCol); // Gives>"656884" - the right answer from db
                                }
                         trace(uidCol); // Gives>"656884" - the right answer from db
                    }
                trace(uidCol); // Gives>null              
           }
            trace(uidCol); // Gives>null         
            return temp //returns a empty object, with uidCol undefined.
      }

      //////////////////////////////////////////////////////////////////////////////


      thanks in advance, snowscreen
        • 1. Re: Help with retrieving data from SQL with server side PHP-Script (Async Token)
          run,ryan! Level 3

          flex is event driven, I believe the trace is outputted by the following order

                                     trace(uidCol); // 4th output
                                 }                                           
                                 trace(uidCol); // 5th output
                                    }
                             trace(uidCol); // 6th output
                        }
                    trace(uidCol); // 1st output
               }
                trace(uidCol); // 2nd output
                return temp //3rd output, return value, an empty object obviosly 
          }
          
          /////////////////////////////////////////////////////////////////////
          
          • 2. Re: Help with retrieving data from SQL with server side PHP-Script (Async Token)
            snowscreen Level 1

            thanks for the quick answer run,ryan!

            Before posting, I had naturally numbered the trace statements. (now I see, that it would have been better to display that too )

            In fact it is this order:

                                       trace(uidCol); // 3th output
                                   }                                          
                                   trace(uidCol); // 2nd output
                                      }
                               trace(uidCol); // 1th output
                          }
                      trace(uidCol); // 5th output - empty
                 }
                  trace(uidCol); // 4th output -empty
                  return temp // also 4th -empty
            }

            So I don't understand why the data "gets lost", or how to solve this problem...

            please provide further help if you can

            thanks in advance, snowscreen


            EDIT: Trying to cut the function into small bits, having fist succes.. will be posting, when solved. But feel free to come with new, better ideas to make this whole thing possible!
            • 3. Re: Help with retrieving data from SQL with server side PHP-Script (Async Token)
              ntsiii Level 3

              First, in your handler, verify that you have the data you think.

               

              var xmlData:XML = XML(e.data);

              trace xmlData.toXMLString();

              • 4. Re: Help with retrieving data from SQL with server side PHP-Script (Async Token)
                snowscreen Level 1

                @ntssi: I assure you, the data is/was there in the returned xml

                 

                But now i managed a way to get the right object "returned":

                 

                 

                private function getThis(searchString:String):void
                {
                    /**
                     * find the order parameters
                     */
                    
                    var desc:Boolean = false;
                    var orderField:String = '';   

                 

                    var parameters:* =
                    {
                        "orderField": orderField,
                        "orderDirection": (desc) ? "DESC" : "ASC",
                        "filter": searchString
                    }
                    /**
                     * execute the server "select" command
                     */
                    searchRequest("FindAll", parameters);
                  
                 
                        function searchRequest(method_name:String, parameters:Object):void
                        {
                        // add the method to the parameters list
                        parameters['method'] = method_name;
                   
                        gateway.request = parameters;
                   
                        var call:AsyncToken = gateway.send();
                        call.request_params = gateway.request;
                        call.handler = searchReqHandler

                 

                        }    

                 


                }

                 


                private function searchReqHandler(e:Object):void
                            {
                                if (e.isError)
                                {
                                    Alert.show("Error: " + e.data.error);
                                }
                                else
                                {      

                 

                                    for each(var row:XML in e.data.row)
                                    {                       
                                        for (var key:String in fields)
                                        {
                                            temp[key + 'Col'] = row[key];
                                        }                   
                                    }   
                                }
                               search = new Event("se"+friendCount);
                               dispatchEvent(search);   //dispatching a unique event in order to get the right result when running getThis() in a loop   

                }

                 

                LESSONs LEARNED

                1# Don't store your result in a too "local" variable when you're dependant on event handlers

                2# A way to establish a straight data returning in event-driven xmlrequests is to dispatch unique events, assuring that the data lands where it belongs.

                3# Don't nest your handlers(?)

                 

                thanks for all your help, snowscreen