11 Replies Latest reply on Jul 28, 2010 10:55 AM by Matt Le Fevre

    How I can transfer data from the database into a variable (or array)?

    Astraport2012 Level 1

      I made my application according to the example (http://corlan.org/2009/06/12/working-in-flash-builder-4-with-flex-and-php/). Everything works fine. I changed one function to query the database - add the two parameters and get the value of the table in String format. A test operation shows that all is ok. If I want to display this value in the text area, I simply drag and drop service to this element in the design mode

      (<s:TextArea x="153" y="435" id="nameText" text="{getDataMeanResult.lastResult[0].name}"  width="296" height="89"  />).

       

      It also works fine, just a warning and encouraged to use ArrayCollection.getItemAt().

       

      Now I want to send the value to a variable or array, but in both cases I get an error: TypeError: Error #1010: A term is undefined and has no properties..

      How can I pass a value from the database into a variable? Thank you.

       

       

      public var nameTemp:String;

      getDataMeanResult.token = authors.getDataMean(arrayOfNumber[0], dir_id);

      nameTemp = getDataMeanResult.lastResult[0].name;

       

       

      public var nameArray:Array = new Array();

      for (var i:uint=o; i<3; i++){

      getDataMeanResult.token = authors.getDataMean(arrayOfNumber[i], dir_id);

      nameArray[i] = getDataMeanResult.lastResult[0].name;

      }

       

      And how i can use syntax highlighting in this forum?

        • 1. Re: How I can transfer data from the database into a variable (or array)?
          rootsounds Level 4

          Sadly, there's no syntax highlighting on here.

           

          I haven't used AMF, but the tutorial shows that you can configure the return type to be a custom class which sounds like what you're after and is a smart way to go.

          1 person found this helpful
          • 2. Re: How I can transfer data from the database into a variable (or array)?
            Astraport2012 Level 1

            Thank you.

            Then this simple question: how i can pass a data from the mySQL database into a variable using the wizards FB4 (connect with PHP)?

            If you can show examples please.

            I could not find examples. Send data to DataGrid or text field - much, but not to variable.

            • 3. Re: How I can transfer data from the database into a variable (or array)?
              Matt Le Fevre Level 4

              Astraport2012 wrote:

               

              Thank you.

              Then this simple question: how i can pass a data from the mySQL database into a variable using the wizards FB4 (connect with PHP)?

              If you can show examples please.

              I could not find examples. Send data to DataGrid or text field - much, but not to variable.

              using the wizards? not sure....it's usually a bad idea to rely on them, as they'll never do fully what you want anyway. I'd recommend learning abit of PHP and handling it by yourself, as it allows you to handle everything exactly the way you want.

               

              quick example of communicating with a database using PHP + FB4:

               

              get a database up

               

              db.jpg

               

              write yourself a small script that'll access your database, pull a value out, for example:

               

              <?php
              
                  mysql_connect("localhost", "username", "password");  
                  mysql_select_db("db");
                  
                  $result = mysql_query("SELECT * FROM chat");
              
                  print "<root>";
                  
                  while($row = mysql_fetch_array($result))
                  {
                      if($row['messageID'] == "3")
                      {
                          print $row['message'];
                      }
                  }
                  print "\n</root>";
              ?>
              

               

              this will get the message with the ID "3"

               

              make a HTTPService within your FB4 app to call this Script, like so:

               

              <?xml version="1.0" encoding="utf-8"?>
              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
                             xmlns:s="library://ns.adobe.com/flex/spark" 
                             xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
                  <fx:Declarations>
                      <!-- Place non-visual elements (e.g., services, value objects) here -->
                      
                      <s:HTTPService id="testService" result="testService_resultHandler(event)" resultFormat="e4x" url="http://localhost/test.php" method="POST"/>
                  </fx:Declarations>
                  
                  <fx:Script>
                      <![CDATA[
                          import mx.rpc.events.ResultEvent;
                          
                          private function dothis():void
                          {
                              testService.send();
                          }
              
                          protected function testService_resultHandler(event:ResultEvent):void
                          {
                              var resultFromDataBase:String = event.result.toString();
                              
                              trace('result: ' + resultFromDataBase)
                          }
              
                      ]]>
                  </fx:Script>
                  <s:Button x="196" y="301" label="Button" click="dothis()"/>
                  
              </s:Application>
              

               

              and the value should be picked up from the Script, and successfully stored in a variable

               

              output.jpg

              • 4. Re: How I can transfer data from the database into a variable (or array)?
                Astraport2012 Level 1

                I think this is exactly what I need:) Thank you very much Matt

                I would glad to not use the wizard, but all the tutorials offered exactly this way.

                • 5. Re: How I can transfer data from the database into a variable (or array)?
                  Astraport2012 Level 1

                  Another small question:)

                  And how you can send parameters from Flex in your example?

                  I need to query the database WHERE like this

                   

                  $query = "SELECT name FROM files WHERE id='".$id."' AND dir_id='".$dir_id."'";

                   

                  My param is $id and $dir_id.

                  • 6. Re: How I can transfer data from the database into a variable (or array)?
                    Matt Le Fevre Level 4

                    Astraport2012 wrote:

                     

                     

                    Another small question:)

                    And how you can send parameters from Flex in your example?

                    I need to query the database WHERE like this

                     

                    $query = "SELECT name FROM files WHERE id='".$id."' AND dir_id='".$dir_id."'";

                     

                    My param is $id and $dir_id.

                     

                    you need to POST them through the HTTPService

                     

                    db.jpg

                     

                    and then your PHP script can retrieve them via the $_POST variable, something like:

                     

                    <?php
                    
                    $username = $_POST["Item1"];
                    
                    $query = "SELECT * FROM table WHERE username = '$username'"
                    
                    etc...
                    

                     

                     

                    i do have some flex -> PHP -> Database examples on my website, it's Flex 3 but it's the same principle

                     

                    http://www.mattlefevre.com/viewExample.php?tut=flexPHP&proj=Simple%20Login%20Application

                     

                     

                    and i have an example of FB4 & PHP interaction here (it doesn't involve databases, but it does show you how to POST data & handle it in PHP)

                     

                    http://www.mattlefevre.com/viewExample.php?tut=flash4PHP&proj=Using%20Cookies

                    1 person found this helpful
                    • 7. Re: How I can transfer data from the database into a variable (or array)?
                      Astraport2012 Level 1

                      All clear

                      Very useful examples of using the cookie and user login. I am also going to use it in my projects.

                      Thanks Matt!

                      • 8. Re: How I can transfer data from the database into a variable (or array)?
                        Astraport2012 Level 1

                        I have to go back to the discussion. The above example works fine when i want to get a single value of the database. But i need to pass an array and get an array, because i want to get at once all the values for all pictures tooltips. I rewrote the proposed Matt PHP-script and it works. However, i can not display the resulting array.

                         

                        My new php:

                        <?php

                        $id = $_POST["pic_id"];

                        $dir_id = $_POST["dir_id"];


                        mysql_connect("localhost", "***", "***"); 

                           mysql_select_db("db");

                        $arr = array();


                        for ($i=0; $i < 3; $i++){


                        $result = mysql_query("SELECT name FROM files WHERE id='$id[$i]' AND dir_id='$dir_id'");

                            $row = mysql_fetch_array($result);

                            $arr[$i] = $row['name'];

                        }

                            print $arr;

                         

                        ?>

                         

                         

                        My send params:

                        <s:HTTPService id="testService" result="testService_resultHandler(event)"

                           resultFormat="e4x" url="http://localhost/names-debug/names.php" method="POST">

                        <s:request xmlns="">

                        <pic_id>

                        arrayOfNumber

                        </pic_id>

                        <dir_id>

                        7

                        </dir_id>

                        </s:request>

                        </s:HTTPService>

                        </fx:Declarations>

                        • 9. Re: How I can transfer data from the database into a variable (or array)?
                          Matt Le Fevre Level 4

                          Astraport2012 wrote:

                           

                          I have to go back to the discussion. The above example works fine when i want to get a single value of the database. But i need to pass an array and get an array, because i want to get at once all the values for all pictures tooltips. I rewrote the proposed Matt PHP-script and it works. However, i can not display the resulting array.

                           

                           

                          yep, it won't work for Arrays, you'll have to do something slightly more intelligent for them.

                           

                          easiest way would be to get your PHP to generate XML, then read that into something like an ArrayList on your HTTPService result event (depends what you're doing with it).

                           

                          for example, you could have the PHP generate XML such as:

                           

                          <pictures>
                               <location>test1.png</location>
                               <location>test2.png</location>
                               <location>test3.png</location>
                               <location>test4.png</location>
                               <location>test5.png</location>
                               <location>test6.png</location>
                          </pictures>
                          

                           

                          then you'll read that in as the ResultEvent, and perform something like this on it

                           

                          private var tempAC:ArrayList = new ArrayList
                          
                          protected function getStuff_resultHandler(event:ResultEvent):void
                          {
                              for each(var item:Object in event.result.pictures)
                              {
                                 var temp:String = (item.@location).toString();
                                 tempAC.addItem(temp);
                              }
                          }
                          

                           

                          in my example on cookies

                           

                          http://www.mattlefevre.com/viewExample.php?tut=flash4PHP&proj=Using%20Cookies

                           

                           

                          you'll see an example of how to format an XML structure containing multiple values:

                           

                          if($_COOKIE["firstName"])
                              {
                                  print "<stored>true</stored>";
                                  
                                  print "<userInfo>
                                          <firstName>".$_COOKIE["firstName"]."</firstName>
                                          <lastName>".$_COOKIE["lastName"]."</lastName>
                                          <userAge>".$_COOKIE["userAge"]."</userAge>
                                          <gender>".$_COOKIE["gender"]."</gender>
                                         </userInfo>";
                              }
                              else
                              {
                                  print "<stored>false</stored>";
                              }
                          

                           

                          which i handle like so

                           

                          if(event.result.stored == true)
                                          {
                                              entryPanel.title = "Welcome back " + event.result.userInfo.firstName + " " + event.result.userInfo.lastName;
                                              firstName.text = event.result.userInfo.firstName;
                                              lastName.text = event.result.userInfo.lastName;
                                              userAge.value = event.result.userInfo.userAge;
                                              userGender.selectedIndex = event.result.userInfo.gender;
                                         }
                          

                           

                           


                          depends on what type of Array you're after

                           

                           

                          from the sounds of it (with the mention of picture tooltips) you're trying to create a gallery with an image, and a tooltip.

                           

                          so i'd probably adopt something like

                           

                          <picture>
                               <location>example1.png</location>
                               <tooltip>tooltip for picture #1</tooltip>
                          </picture>
                          <picture>
                               <location>example2.png</location>
                               <tooltip>tooltip for picture #2</tooltip>
                          </picture>
                          <picture>
                               <location>example3.png</location>
                               <tooltip>tooltip for picture #3</tooltip>
                          </picture>
                          etc...
                          

                           

                          or

                           

                          <picture location="example1.png" tooltip="tooltip for picture #1"/>
                          <picture location="example2.png" tooltip="tooltip for picture #2"/>
                          <picture location="example3.png" tooltip="tooltip for picture #3"/>
                          etc...
                          
                          • 10. Re: How I can transfer data from the database into a variable (or array)?
                            Astraport2012 Level 1

                            And again, thanks Matt.

                            Your method is not universal, because I use different states (more 50) with different numbers of images (about 30 or more). But I will try to apply your advice to my case.

                            • 11. Re: How I can transfer data from the database into a variable (or array)?
                              Matt Le Fevre Level 4

                              Astraport2012 wrote:

                               

                              And again, thanks Matt

                              Your method is not universal, because I use different states (more 50) with different numbers of images (about 30 or more). But I will try to apply your advice to my case.

                              no worries

                               

                              if you get stuck and want a more specific solution, you'd have to post more detail about what you're up to