By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

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

P: 75
hai,
I created a database using the SQL command
Expand|Select|Wrap|Line Numbers
  1.  CREATE TABLE login_table2(user_name VARCHAR(32), first_name VARCHAR(32), 
  2.     last_name VARCHAR(32), password VARCHAR(64));
Then i inserted a data using the command below
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO login_table2(user_name ,first_name , last_name , password ) 
  2.     VALUES('ramya', 'ramya', 'muthu', 'India'); 
the data got inserted into the table.
Then i inserted another set of data using the command mentioned below.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO login_table2(user_name ,first_name , last_name , password ) 
  2.     VALUES('jeyshree', 'jey', 'shree', 'India');
Again the data got inserted into the table too.
Then i gave the command below
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1. SELECT password FROM login_table2 WHERE user_name = 'ramya';
Mention where i am going wrong.Awaiting your reply.Thanks in advance
Jul 3 '13 #1
Share this Question
Share on Google+
9 Replies


Rabbit
Expert Mod 10K+
P: 12,364
Why in your fourth block of code do you use first name but in your last block of code you use user name?
Jul 3 '13 #2

P: 75
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
Jul 3 '13 #3

Rabbit
Expert Mod 10K+
P: 12,364
And what is the result set returned for the modified query in the fourth block of code?
Jul 3 '13 #4

P: 75
For the fourth block of code it returns
"ramya
jeyshree".
For the fifth block of code it returns nothing
Jul 4 '13 #5

P: 75
For the fourth block of code it returns
"ramya
jeyshree".
For the fifth block of code it returns nothing
Jul 4 '13 #6

Rabbit
Expert Mod 10K+
P: 12,364
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.
Jul 4 '13 #7

P: 75
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
Jul 4 '13 #8

P: 75
hai,
i used a different form of select command to verify user name and password.the code is shown below
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>login</title>
  4. </head>
  5. <body>
  6. <form action="" method="post">
  7. <strong>user_name: *</strong> <input type="text"  name="user_name">
  8. <strong>password: *</strong> <input type="text"  name="password">
  9.    <input type="submit" name="submit" value="Submit">
  10. </form>
  11. <?php
  12. if (isset($_POST['user_name'])) {
  13.         $user_name = htmlentities($_POST['user_name']);
  14.         echo 'The user name is ' . $user_name . '<br>';
  15.     }    
  16. if (isset($_POST['password'])) {
  17.         $password1 = htmlentities($_POST['password']);
  18.         echo 'The code is ' . $password1 . '<br>';
  19.     }
  20. $host = "localhost";
  21. $user = "root";
  22. $password = "";
  23. if (!$connection = mysql_connect($host,$user,$password))
  24. {
  25. $message = mysql_error();
  26. echo ".$message";
  27. echo"\nnot connected to server.try later";
  28. die();
  29. }
  30. else
  31. echo" connected to server";
  32. $database = "database_1";
  33. $db = mysql_select_db($database,$connection) or die ("Couldn’t select database.");
  34. if(!$db)
  35. echo"<br>fail in database connection";
  36. else
  37. echo"<br>successfully connected to database";
  38. $query = 'SELECT user_name,password
  39.         FROM login_table2';
  40. $result = mysql_query($query) or die ("duplicated user name.try some other");
  41. if(! $result )
  42. {
  43.   die('Could not get data: ' . mysql_error());
  44. }
  45.  echo '<br>';
  46. $match=1;    
  47. while($row = mysql_fetch_assoc($result))
  48. {
  49. $value1= $row['user_name']; 
  50. $value2= $row['password'];  
  51. if($value1== $user_name )
  52. {
  53.            $match=0;           
  54.            if($value2== $password1 )
  55.           {
  56.                 echo 'user name and password matches<br>';
  57.            } 
  58.          else
  59.          {
  60.                 echo 'user name matched.password mismatch<br>';
  61.          }
  62.          break;
  63. }
  64. if($match=='1')
  65. {
  66. echo 'user name not available<br>';
  67. }
  68. mysql_close($connection);
  69. ?>
  70.  
  71. </body>
  72. </html>
  73.  
When i give user name and password as 123 and 123 respectively
the code outputs"user name and password matches".However, 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.
Jul 7 '13 #9

Rabbit
Expert Mod 10K+
P: 12,364
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.
Jul 7 '13 #10

Post your reply

Sign in to post your reply or Sign up for a free account.