Simple SELECT query not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • captainmerton
    New Member
    • May 2007
    • 12

    Simple SELECT query not working

    Am i losing my mind? The following mysql query seems to fail even though i am running it outside of the PHP code and its working fine. An echo statement shows that the select variables are being populated as expected. can anyone advise?
    [PHP]
    $userid = $_POST['userid'];
    $password = $_POST['password'];

    $getuser = "SELECT UserID,Password FROM User WHERE UserID = '$userid' AND Password = '$password'";

    $userlogin=mysq l_query($getuse r);

    while ($row = mysql_fetch_arr ay($userlogin)) {
    $loginid = $row['UserId'];
    $loginpwd = $row['Password']; }

    if (!$userlogin) {
    exit('<p>There was a problem recalling your user details<p>');
    }
    [/PHP]
    Last edited by Atli; May 13 '07, 08:14 PM. Reason: Added code tags
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Have you check if the mysql_error() function gives you any error messages?
    Can you give use the table structure?
    If you have any error messages, please share them with us.

    Besides everything, you do realize that statment is giving you the excact same data you are puting into it, right?

    O, and I just realized, your putting quote marks arround your UserID. Are you sure you should be doing that?

    Comment

    • captainmerton
      New Member
      • May 2007
      • 12

      #3
      Thanks for replying Atli.

      The table structure is:

      UserID char(10)
      Password char(10)
      DisplayName char(30)

      UserID is the primary key.

      I am aware I am returning what I am putting in. I will change that. All i am doing is attempting to pull back a row given the 2 SELECT conditions. How do I get the error text out. I have used the following command and nothing is displayed:

      $error=mysql_er ror($userlogin) ;

      echo $error;

      Comment

      • hugonot
        New Member
        • May 2007
        • 4

        #4
        TRY with
        Code:
        $getuser = "SELECT UserID,Password FROM User WHERE (UserID = '$userid' AND Password = '$password')";

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          If you are using UserID as a primary key, you dont want the quote marks arround it in your WHERE clause. The quote marks are ment for strings, ID's are integers.

          You are using the mysql_error() function incorrectly, here is an example of how to use it.
          [PHP]
          // Connect to database
          $DB = @mysql_connect( "localhost" , "user", "pass") or die("Connection failed: ". mysql_error());
          @mysql_select_d b("myDB", $DB) or die("Database selection failed: ". mysql_error($DB ));

          // Build query
          $SQL = "SELECT * FROM myTable";

          // Execute query
          $RESULT = @mysql_query($S QL, $DB);

          // Check results
          if(!!$RESULT) {
          // Query OK. Do stuff!
          }
          else {
          // Query Failed. Print error
          echo "SQL Query failed:
          <blockquote>$SQ L</blockquote>
          <blockquote>" . mysql_error($DB ) ."</blockquote>";

          }
          [/PHP]

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Originally posted by hugonot
            TRY with
            Code:
            $getuser = "SELECT UserID,Password FROM User WHERE (UserID = '$userid' AND Password = '$password')";
            Putting (..) arround the conditions in this query does nothing. They are used to group conditions together in more complex queries.

            For example:
            Code:
            SELECT * FROM usertbl
            WHERE
              (ID = 1 OR Name = 'ATLI') 
            AND 
              (Password = sha('Password') OR isLoggedIN = true)
            This query is far from perfect, but you get my point.

            Comment

            • ak1dnar
              Recognized Expert Top Contributor
              • Jan 2007
              • 1584

              #7
              Originally posted by captainmerton
              Am i losing my mind? The following mysql query seems to fail even though i am running it outside of the PHP code and its working fine. An echo statement shows that the select variables are being populated as expected. can anyone advise?
              [PHP]
              $userid = $_POST['userid'];
              $password = $_POST['password'];

              $getuser = "SELECT UserID,Password FROM User WHERE UserID = '$userid' AND Password = '$password'";

              $userlogin=mysq l_query($getuse r);

              while ($row = mysql_fetch_arr ay($userlogin)) {
              $loginid = $row['UserId'];
              $loginpwd = $row['Password']; }

              if (!$userlogin) {
              exit('<p>There was a problem recalling your user details<p>');
              }
              [/PHP]
              This coding is working perfectly with my Tables.
              [PHP]<?php
              require 'dbcon.php';
              $userid = 1;
              $password = 'Mike';
              $getuser = "SELECT region,staffid FROM users WHERE id = '$userid' AND firstname = '$password'";
              $userlogin=mysq l_query($getuse r) or die (mysql_error()) ;

              while ($row = mysql_fetch_arr ay($userlogin))
              {
              echo $loginid = $row['region'];
              echo $loginpwd = $row['staffid'];
              }
              if (!$userlogin)
              {
              exit('<p>There was a problem recalling your user details<p>');
              }
              ?>[/PHP]

              with some sample values to the POST Strings. if there you getting a problem check your HTML form also.
              You can do it by simply echoing the POST strings and also the Sql String.

              [PHP]echo $userid;
              echo $password;
              echo $getuser;
              [/PHP]

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Simple question: have you connected to your database?

                Comment

                • captainmerton
                  New Member
                  • May 2007
                  • 12

                  #9
                  Motoma - you are a genius. And I am a moron. I had forgotton to call the include file making the database connection. how embarassing. In any case thanks for your help everyone.

                  Comment

                  Working...