6 Replies Latest reply on Mar 30, 2009 12:33 AM by Newsgroup_User

    Flex + php + mySQL query problem

    Level 7
      Hi all,

      I'm working on a flex project wich has to get a XMLList from a mySQL
      database by php. I can't do a SELECT * FROM Table twice, just the first time
      it gets the correct values... I can insert an other row in my db, but I
      can't update a dataGrid never more. I'm getting crazy...
      If I do an insertRecord() before to updateDB()... the insert always works,
      but the update just works for the first time again.

      the flex code:

      <mx:HTTPService id = "httpService"
      url=http://www.myserver.com/myphp.php
      showBusyCursor="true"
      fault="onFaultHandler(event);"
      resultFormat="e4x"
      useProxy="false"
      />

      <mx:Script>
      <![CDATA[
      include "funcs.as";
      ]]>
      </mx:Script>
      <mx:TextInput id="_name"/>
      <mx:TextInput id="_year"/>
      <mx:TextInput id="_sizeX"/>
      <mx:TextInput id="_sizeY"/>
      <mx:Button label="Insert Record" click="insertRecord()"/>

      <mx:DataGrid x="57" y="232" width="293" height="214" textAlign="center"
      editable="false"
      dataProvider="{dataGridTotal}" id="_dataGrid">
      <mx:columns>
      <mx:DataGridColumn headerText="id" dataField="id"/>
      <mx:DataGridColumn headerText="name" dataField="name"/>
      <mx:DataGridColumn headerText="year" dataField="year"/>
      <mx:DataGridColumn headerText="sizeX" dataField="sizeX"/>
      <mx:DataGridColumn headerText="sizeY" dataField="sizeY"/>
      </mx:columns>
      </mx:DataGrid>

      <mx:Button label="Update DDBB" click="updateDB()"/>


      my funcs.as code:

      public function updateDB():void // It works just the first
      time is executed. never more. WHY????
      {
      dataGridTotal = new XMLList;
      httpService.addEventListener(ResultEvent.RESULT,onResultHandler);
      httpService.method = "GET";
      var _params:Object = new Object();
      _params = {"method": "getAll"};
      httpService.cancel();
      httpService.send(_params);
      }

      public function onResultHandler(event:ResultEvent):void
      {
      httpService.removeEventListener(ResultEvent.RESULT,onResultHandler);
      dataGridTotal = event.result.item as XMLList;
      }

      public function insertRecord():void
      {
      httpService.addEventListener(ResultEvent.RESULT,onInsertItemResultHandler);
      httpService.method = "POST";
      var _params:Object = new Object();
      _params = {"method": "insertRecord", "id": NaN, "name": _name.text,
      "year": _year.text, "sizeX": _sizeX.text, "sizeY": _sizeY.text};
      httpService.cancel();
      httpService.send(_params);
      clearInsertInputFields();
      }

      public function onInsertItemResultHandler(event:ResultEvent):void
      {
      httpService.removeEventListener(ResultEvent.RESULT,onInsertItemResultHandler);
      updateDB(); // don't do that
      }



      the php code:


      <?php
      $conn = mysql_pconnect("myserver", "user", "pass");
      if (!$conn) {
      exit("Unable to connect to DB: " . mysql_error());
      }
      if (!mysql_select_db("myDB")) {
      exit("Unable to select mydbname: " . mysql_error());
      }

      switch (@$_REQUEST["method"])
      {
      case "getAll":
      $ret = getAll();
      break;
      case "insertRecord":
      $ret = insertRecord();
      break;
      }
      mysql_close($conn);
      echo $ret;

      function insertRecord()
      {
      global $conn;
      $name = @$_REQUEST["name"];
      $year = @$_REQUEST["year"];
      $sizeX = intval(@$_REQUEST["sizeX"]);
      $sizeY = intval(@$_REQUEST["sizeY"]);
      $sql = "INSERT INTO Test (name, year, sizeX, sizeY) VALUES ('$name',
      '$year', '$sizeX', '$sizeY')";
      $result = mysql_query($sql, $conn);
      return $result;
      }

      function getAll()
      {
      global $conn;
      $sql = "SELECT * FROM Test";
      $result = mysql_query($sql, $conn);
      if (!$result) {
      echo "Could not successfully run query ($query_recordset) from DB:
      " . mysql_error();
      exit;
      }
      else {
      $xmlStr = "<output>";
      while ($row = mysql_fetch_assoc($result)) {
      $xmlStr.= "<item>";
      for($x = 0 ; $x < mysql_num_fields($result) ; $x++) {
      $fieldName = mysql_field_name($result,$x);
      $xmlStr.=
      "<".$fieldName.">".$row[$fieldName]."</".$fieldName.">";
      }
      $xmlStr.= "</item>";
      }
      $xmlStr.= "</output>";
      mysql_free_result($result);
      return $xmlStr;
      }
      }
      ?>


        • 1. Re: Flex + php + mySQL query problem
          Richard_Abbott Level 3
          LAPR,
          Are you allowed to write
          dataGridTotal = new XMLList;
          rather than
          dataGridTotal = new XMLList();

          Richard
          • 2. Re: Flex + php + mySQL query problem
            Level 7
            yeah... you are right :P.
            But it doesn't change anything, the problem still there...
            thanks anyway

            lapr.

            "Richard_Abbott" <webforumsuser@macromedia.com> escribió en el mensaje
            news:gqicip$518$1@forums.macromedia.com...
            > LAPR,
            > Are you allowed to write
            > dataGridTotal = new XMLList;
            > rather than
            > dataGridTotal = new XMLList();
            >
            > Richard


            • 3. Re: Flex + php + mySQL query problem
              Gregory Lafrance Level 6
              I'm wondering if something is going wrong because you are adding the event listener in the updateDB() method and removing it in the result handler functions. Maybe just add the handler in the HTTPService definition. Why are you removing it each time?

              <mx:HTTPService id = "httpService"
              url=http://www.myserver.com/myphp.php
              showBusyCursor="true"
              fault="onFaultHandler(event);"
              resultFormat="e4x"
              useProxy="false"
              result="onResultHandler"
              />
              • 4. Re: Flex + php + mySQL query problem
                Level 7
                well... I do that because there are two different handler events for the
                results:
                onResultHandler for the updateDB()
                onInsertItemResultHandler for the instertRecord()

                I've tried to add the listener in the HTTPService definition... just
                thinking in updateDB(), but it still retrieving the correct values for the
                first time.

                :(

                thanks again!
                lapr


                "Greg Lafrance" <webforumsuser@macromedia.com> escribió en el mensaje
                news:gqifnl$8im$1@forums.macromedia.com...
                > I'm wondering if something is going wrong because you are adding the event
                > listener in the updateDB() method and removing it in the result handler
                > functions. Maybe just add the handler in the HTTPService definition. Why
                > are
                > you removing it each time?
                >
                > <mx:HTTPService id = "httpService"
                > url=http://www.myserver.com/myphp.php
                > showBusyCursor="true"
                > fault="onFaultHandler(event);"
                > resultFormat="e4x"
                > useProxy="false"
                > result="onResultHandler"
                > />
                >
                >


                • 5. Re: Flex + php + mySQL query problem
                  Level 7

                  "LAPR" <aperez@bcnmultimedia.cat> wrote in message
                  news:gqi8sb$m7$1@forums.macromedia.com...
                  > Hi all,
                  >
                  > I'm working on a flex project wich has to get a XMLList from a mySQL
                  > database by php. I can't do a SELECT * FROM Table twice, just the first
                  > time it gets the correct values... I can insert an other row in my db, but
                  > I can't update a dataGrid never more. I'm getting crazy...
                  > If I do an insertRecord() before to updateDB()... the insert always works,
                  > but the update just works for the first time again.

                  http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf
                  Q7


                  • 6. Re: Flex + php + mySQL query problem
                    Level 7
                    :) yeah!!!!! it works...

                    The browser has cached the response it got from the first call you made to
                    the page. This means that you need to either change how you are calling the
                    page so it appears to be a new request, or you need to change the page
                    itself so that it won't be cached. To change how you are calling the page,
                    append a unique value to the end of the

                    URL. For example, new URLRequest('yourPage.asp?param="+(new Date()).getTime)


                    public function updateDB():void
                    {
                    httpService.addEventListener(ResultEvent.RESULT,onResultHandler);
                    httpService.method = "GET";
                    var _params:Object = new Object();
                    _params = {"method": "getAll", "date": (new Date()).getTime()};
                    httpService.cancel();
                    httpService.send(_params);
                    }

                    ...thanks a lot!!!

                    lapr



                    "Amy Blankenship" <amySpamFilter@magnolia_pleaseNOspam_multimedia.com>
                    escribió en el mensaje news:gqisbe$nq8$1@forums.macromedia.com...
                    >
                    > "LAPR" <aperez@bcnmultimedia.cat> wrote in message
                    > news:gqi8sb$m7$1@forums.macromedia.com...
                    >> Hi all,
                    >>
                    >> I'm working on a flex project wich has to get a XMLList from a mySQL
                    >> database by php. I can't do a SELECT * FROM Table twice, just the first
                    >> time it gets the correct values... I can insert an other row in my db,
                    >> but I can't update a dataGrid never more. I'm getting crazy...
                    >> If I do an insertRecord() before to updateDB()... the insert always
                    >> works, but the update just works for the first time again.
                    >
                    > http://www.magnoliamultimedia.com/flex_examples/Amys_Flex_FAQ.pdf
                    > Q7
                    >