select command in mysql returns an empty row even though the entry exist in table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeyshree
    New Member
    • Jul 2010
    • 75

    select command in mysql returns an empty row even though the entry exist in table

    hai,
    I created a database using the SQL command
    Code:
     CREATE TABLE login_table2(user_name VARCHAR(32), first_name VARCHAR(32), 
        last_name VARCHAR(32), password VARCHAR(64));
    Then i inserted a data using the command below
    Code:
    INSERT INTO login_table2(user_name ,first_name , last_name , password ) 
        VALUES('ramya', 'ramya', 'muthu', 'India');
    the data got inserted into the table.
    Then i inserted another set of data using the command mentioned below.
    Code:
    INSERT INTO login_table2(user_name ,first_name , last_name , password ) 
        VALUES('jeyshree', 'jey', 'shree', 'India');
    Again the data got inserted into the table too.
    Then i gave the command below
    Code:
    SELECT user_name FROM login_table2;
    The above command displayed all the user_ name in the table.
    However when i gave the command below it does not fetch anything though the entry exist in the table.
    Code:
    SELECT password FROM login_table2 WHERE user_name = 'ramya';
    Mention where i am going wrong.Awaiting your reply.Thanks in advance
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Why in your fourth block of code do you use first name but in your last block of code you use user name?

    Comment

    • jeyshree
      New Member
      • Jul 2010
      • 75

      #3
      sir/mam,
      that was just an example to show u that commands in such syntax works.but actually i want the password for that particular user name.i edited the post again.please take a look

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        And what is the result set returned for the modified query in the fourth block of code?

        Comment

        • jeyshree
          New Member
          • Jul 2010
          • 75

          #5
          For the fourth block of code it returns
          "ramya
          jeyshree".
          For the fifth block of code it returns nothing

          Comment

          • jeyshree
            New Member
            • Jul 2010
            • 75

            #6
            For the fourth block of code it returns
            "ramya
            jeyshree".
            For the fifth block of code it returns nothing

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              If everything you posted is accurate, then I see no reason for the results you're seeing. I am of course assuming you posted everything exactly. Capitalization, spelling, punctuation exactly the same way you were running the code and what you saw as results.

              Comment

              • jeyshree
                New Member
                • Jul 2010
                • 75

                #8
                if everything is right then y i am not able to find the user name and the respective password.if u know some other syntax please let me know

                Comment

                • jeyshree
                  New Member
                  • Jul 2010
                  • 75

                  #9
                  hai,
                  i used a different form of select command to verify user name and password.the code is shown below
                  Code:
                  <html>
                  <head>
                  <title>login</title>
                  </head>
                  <body>
                  <form action="" method="post">
                  <strong>user_name: *</strong> <input type="text"  name="user_name">
                  <strong>password: *</strong> <input type="text"  name="password">
                     <input type="submit" name="submit" value="Submit">
                  </form>
                  <?php
                  if (isset($_POST['user_name'])) {
                          $user_name = htmlentities($_POST['user_name']);
                          echo 'The user name is ' . $user_name . '<br>';
                      }    
                  if (isset($_POST['password'])) {
                          $password1 = htmlentities($_POST['password']);
                          echo 'The code is ' . $password1 . '<br>';
                      }
                  $host = "localhost";
                  $user = "root";
                  $password = "";
                  if (!$connection = mysql_connect($host,$user,$password))
                  {
                  $message = mysql_error();
                  echo ".$message";
                  echo"\nnot connected to server.try later";
                  die();
                  }
                  else
                  echo" connected to server";
                  $database = "database_1";
                  $db = mysql_select_db($database,$connection) or die ("Couldn’t select database.");
                  if(!$db)
                  echo"<br>fail in database connection";
                  else
                  echo"<br>successfully connected to database";
                  $query = 'SELECT user_name,password
                          FROM login_table2';
                  $result = mysql_query($query) or die ("duplicated user name.try some other");
                  if(! $result )
                  {
                    die('Could not get data: ' . mysql_error());
                  }
                   echo '<br>';
                  $match=1;    
                  while($row = mysql_fetch_assoc($result))
                  {
                  $value1= $row['user_name']; 
                  $value2= $row['password'];  
                  if($value1== $user_name )
                  {
                             $match=0;           
                             if($value2== $password1 )
                            {
                                  echo 'user name and password matches<br>';
                             } 
                           else
                           {
                                  echo 'user name matched.password mismatch<br>';
                           }
                           break;
                  }
                  } 
                  if($match=='1')
                  {
                  echo 'user name not available<br>';
                  }
                  mysql_close($connection);
                  ?>
                  
                  </body>
                  </html>
                  When i give user name and password as 123 and 123 respectively
                  the code outputs"user name and password matches".Howeve r, when i give user name and password as ram and 123 respectively
                  the code outputs"user name not available" even though the entry exists in the table.Can u please say me where i am going wrong?Thanks for ur reply in advance.

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    It most likely means that the values in the table are not what you think it is. You should output the values being returned by the query to see if the values are correct.

                    Comment

                    Working...