1 Reply Latest reply on Jun 2, 2009 5:47 AM by Richard_Abbott

    Single PHP file only way to go?

    Jim Daniel Level 1

      I've run into a problem with using PHP file(s) to do my MySQL quires.  I was attempting to use separate php files and http services to login and fetch data from the db.  Unfortunately this does not work as I thought it would, when I do a query to get data from a Users table I get the 'No database selected” error.  Where can I find out how to solve this problem?  I tried using the db wizard but it not very clear about what it is doing and why and I'd need to make some major changes to how it logs in.

       

      Here are the HTTPSerivce code and the PHP files

       

          <mx:HTTPService id="UserService" result="resultUserHandler(event)" fault="faultHandler(event)" resultFormat="e4x" showBusyCursor="true" method="POST" url="PHP/userInfo.php" useProxy="false">
              <mx:request xmlns="">
                  <username>
                      userName.text
                  </username>
              </mx:request>
          </mx:HTTPService>

       

          <mx:HTTPService id="user_login" result="loginHandler(event)" fault="faultHandler(event)" showBusyCursor="true" method="POST" url="PHP/login.php" useProxy="false">
              <mx:request xmlns="">
                  <username>
                      {UserName.text}
                  </username>
                  <password>
                      {Password.text}
                  </password>
              </mx:request>
          </mx:HTTPService>

       

      Login.php

       

      ?php
          require_once("constants.php");
         
          global $connection;
          global $db_select;

       

          $username = mysql_real_escape_string($_POST['username']);
          $password = mysql_real_escape_string($_POST['password']);

           // 1. Create a database conection
          $connection = mysql_pconnect(DB_SERVER, "root", "root");

       

          $output = "<loginsuccess>";
         
          if(!$connection) {
              $output .= "<message>";
              $output .= mysql_error();
              $output .= "</message>";
             
              $output .= "</loginsuccess>";  
          } else {
              $output .= "yes";
              $output .= "</loginsuccess>";
          }

       

          // 2. Select a database to use
          if($connection) {
              $output .= "<DBSuccess>";

       

              $db_select = mysql_select_db(DB_NAME, $connection);
              if(!$db_select) {
                  $output .= "<message>";
                  $output .= mysql_error();
                  $output .= "</message>";
                  $output .= "</DBSuccess>";  
              } else {
                  $output .= "yes";
                  $output .= "</DBSuccess>";
              }
          }
           print ($output);
      ?>

       

      userInfo.php

       

      <?php

       

          require_once("constants.php");

       

          $username = mysql_real_escape_string($_POST['username']);

       

          $output = "<userInfo>";

       

          $Query = "SELECT * FROM Users WHERE AccounNumber=1";

       

          $Result = mysql_query( $Query );

       

          $output .= "<result>";
         
           if(!$Result) {
              $output .= "<message>";
              $output .= mysql_error();
              $output .= "</message>";
          } else {
              $output .= "success";
          }
          $output .= "</result>";
         
          $output .= "</userInfo>";
          print ($output);

       

      ?>

        • 1. Re: Single PHP file only way to go?
          Richard_Abbott Level 3

          Jim,

          having a quick look at this, it seems to me that the problem is one of retaining state. You have two different pages, one for login and one for actual data retrieval, and I don't think you have anything that retains the login state between pages. This is a very common problem for web-based systems, not just Flex ones. There are two main routes that I have seen used: one is simply to hold the login user credentials in session variables (or equivalent) in the server code, and re-login (and log out as soon as possible) on every page that calls database code. The other is to configure the database to hold the login open for shared use. This of course runs the risk in a multi-user system of running out of resource handles, and my personal preference would be to choose the first option.

          Design-wise, your login php page would validate the credentials and let the user know whether they had succeeded or failed, and then store the credentials somewhere session-permanent. The server will then handle session expiry for you. The db call page would reuse the stored credentials and get the data.

          Hope that makes sense,

          Richard