3 Replies Latest reply on Sep 21, 2010 12:23 PM by jjbaranowski

    Problem with Event Listeners and Database Queries

    jjbaranowski

      I have a question about Event Listeners. I'm using the "addEventListener" function to listen for Results to returned from a database query (using PHP service calls). My goal is to wait for the results to return, then do something with the results. I've included a function that I wanted to execute after the Results are returned to the application (getReferenceResult.addEventListener(ResultEvent.RESULT,FunctionToRunAfter).

       

      This works fine (my function runs after the results are returned) only when the query actually has data to return. If the the query returns nothing, then the function never runs. Is this normal or should the event listener still run the function even if the query fails to return data. Is there a different way to do this and know if the record set is empty?

       

      Thanks,

       

      -John

       

      I'm using Flash builder 4 and ZendFamework / PHP Services

        • 1. Re: Problem with Event Listeners and Database Queries
          John Hall Level 4

          Just to clarify, you suggest that nothing happens if there is no data. So does it actually return null (from the server) or an empty structure or does your function simply never fire? If not, are you handling the case on the server where it is an empty set and returning something that can be trapped? Is it returning an ArrayCollection, if successful? If so, does it not return a zero length ArrayCollection when there are no results?

          • 2. Re: Problem with Event Listeners and Database Queries
            jjbaranowski Level 1

            John,

             

            Thanks for replying! The function that I typed into the addEventListener line never fires. It does fire when the PHP query finds a record. I think I need to make my PHP function return something even if it doesn't find a record. Otherwise the EventListener never sees anything and never fires.I see in my code that I do return "null" if I don't find a record but I cant test anything back in Flex because my event listener never fires my function. Any ideas?

             

             

            public function getRefCode($RefCode) {

             

            $stmt = mysqli_prepare($this->connection, "SELECT field1, field2 FROM TABLE WHERE field1=?");

            $this->throwExceptionOnError();

             

            mysqli_stmt_bind_param($stmt, 's', $RefCode);

            $this->throwExceptionOnError();

             

            mysqli_stmt_execute($stmt);

            $this->throwExceptionOnError();

             

            $rows = array();

             

            mysqli_stmt_bind_result($stmt, $row->field1, $row->field2);

             

            if(mysqli_stmt_fetch($stmt)) {

                  return $row;

            } else {

                  return null;

            }

             

               mysqli_stmt_free_result($stmt);

               mysqli_close($this->connection);  

            • 3. Re: Problem with Event Listeners and Database Queries
              jjbaranowski Level 1

              I did a test; if my recordset was empty (in the PHP code), instead of returning "null" I returned an array I made manually in PHP code and the EventListener back in my flex app fires my function just fine. I'm not sure if this is a bug or not. I can live with this at this point but it would be nice to know how this is supposed to work. If you have any other ideas I would be willing to test them out!

               

              Thanks,

               

              John Baranowski