4 Replies Latest reply on Jul 16, 2010 12:32 AM by mnsmnl4

    Help with HttpService using PHP

    mnsmnl4

      Hi alls,

      I have a problem when using HttpService to get data from mySQL through a PHP page.

      Particularly, I am creating a Log In in Flex. When the user clicks LogIn button, I use HttpService to send username and password to a server-side PHP page. This PHP page will access mySQL db and send back the login's result.

      The result format of HttpService that I used is text. But the result I got is the whole source code of the PHP page.

      I don't know the reason why.

      Can anybody help?

       

      Best regards,

      MNS

        • 1. Re: Help with HttpService using PHP
          BhaskerChari Level 4

          Hi mnsmnl4,

           

          There is some error in your PHP page thats why the result you got is the whole source code of the PHP page.

           

          Make sure that your PHP code is running correctly without errors when the request to it arrives...try to put the echo in your PHP page whether it is executing correctly and returning the result message text correctly..

           

          Thanks,

          Bhasker Chari.S

          1 person found this helpful
          • 2. Re: Help with HttpService using PHP
            mnsmnl4 Level 1

            Hi BhaskerChari,

            Thanks for your response.

            I checked and there is no problem with the PHP page, I think.

            I have posted the source code here. If you guys have time, please have a look on it.

             

            php page:serverSideDBAccess.php

            <?php
                $action = $_REQUEST['action'];

             

                // constant variables
                $dbhost = "127.0.0.1:3306";
                $dbuser = "root";
                $dbpass = "root";
                $dbname = "myDB";

             

                // log in
                if($action = "login")
                {
                    $usn = $_REQUEST['usn'] ;
                      $pas = $_REQUEST['pas'] ;
                     
                      // connect
                      $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
                    mysql_select_db($dbname);
                   
                    // access database
                    $queryStr = "SELECT COUNT(*) FROM Users WHERE username='$usn' AND password='$pas'";
                    $result = mysql_query( $queryStr );
                   
                    // $result = 1: login successfully
                    // else: login fail
                    print $result;
                }
            ?>

             

            mxml file: login.mxml

                      private function btnLogInClick(e: MouseEvent = null) : void
                        {
                            var httpservice : HTTPService = new HTTPService();
                            httpservice.method = "POST";
                            httpservice.url = "http://localhost:8080/serverSideDBAccess.php";
                            httpservice.resultFormat = "text";
                            httpservice.addEventListener(ResultEvent.RESULT, loginResult);
                            httpservice.addEventListener(FaultEvent.FAULT, loginFail);
                           
                            var params : Object = new Object();
                            params.action = "login";
                            params.usn = tiUsername.text; // tiUsername is a textInput control
                            params.pas = tiPassword.text; // tiPassword is a textInput control
                            httpservice.send(params);
                        }
                       
                        private function loginResult ( e : ResultEvent = null) : void
                        {
                           Alert.show("success::" + e.result.toString());
                        }
                       
                        private function loginFail ( e : FaultEvent = null) : void
                        {
                            Alert.show("fail::" + e.message.toString());
                        }

             

            Thanks in advance.

             

            Best regards,

            MNS

            • 3. Re: Help with HttpService using PHP
              Matt Le Fevre Level 4

              have a working example here for reference:

               

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

               

               

              made some corrections/alterations to your PHP code

               

              <?php
                  $action = $_POST['action'];

               

                  // constant variables
                  $dbhost = "127.0.0.1:3306";
                  $dbuser = "root";
                  $dbpass = "root";
                  $dbname = "myDB";

               

                  // log in
                  if($action == "login")
                  {
                      $usn = $_POST['usn'] ;
                        $pas = $_POST['pas'] ;
                       
                       // connect
                        $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
                      mysql_select_db($dbname);
                     
                      // access database
                     $queryStr = "SELECT COUNT(*) FROM Users WHERE username='$usn' AND password='$pas'";
                      $result = mysql_query( $queryStr );
                    
                      // $result = 1: login successfully
                      // else: login fail
                      print $result;
                  }
              ?>

               

              it's a double equals for the if statement, you know what method you're using (POST), so for good practice state so.

               

              s'all i could find from a quick look over

              1 person found this helpful
              • 4. Re: Help with HttpService using PHP
                mnsmnl4 Level 1

                Thanks for your help.

                I solved the problem

                 

                Best regards,

                MNS