Jump to content

SELECT statement not working


atmega-ist

Recommended Posts

hello, all -

 

i've got a SELECT statement that isn't returning any errors but is simply not working.  all test points leading up to the query confirm a connection to the database but selecting a record just doesn't seem to go through.  the intention is simple - just to retrieve the password which correlates to a given username.  i'll admit i'm fairly new to php/mysql but have had quite a bit of access experience in the past...  just doesn't seem to be coming together, though.  thanks in advance

 

I should also mention that the scenario implied by the code below is not at all intended to be deployed.  when thinking of tables of related records it was just the first thing to come to mind in terms of dummy-data.

 

mySQL v5.1 (XAMPP installation)

 

"logintable" consists of 3 fields (id, username, password)

 

<html>
<body>

<form action="login.php" method="POST">
Username: <input type="text" name="userName" value="user2"><br>
<input type="submit" value="Click"><p>
</form>
<?php

$user = $_POST['userName'];

mysql_connect("localhost","root","dbopwd") or die(mysql_error());
mysql_select_db("login") or die(mysql_error());

echo mysql_query("SELECT password FROM logintable WHERE username='$user'");

?>
</body>
</html>

 

Link to comment
Share on other sites

Your can't just echo the query.

You'll need to code it sth like below

 

$result = mysql_query("SELECT password FROM logintable WHERE username='$user'");
$row = mysql_fetch_array($result);
echo $row["password"];

 

 

Link to comment
Share on other sites

As riwan said -

 

echo just prints to the screen. You are not actually querying the database there.

 

<html>
<body>

<form action="login.php" method="POST">
Username: <input type="text" name="userName" value="user2"><br>
<input type="submit" value="Click"><p>
</form>
<?php

$user = $_POST['userName'];

mysql_connect("localhost","root","dbopwd") or die(mysql_error());
mysql_select_db("login") or die(mysql_error());


       $result = mysql_query("SELECT password FROM logintable WHERE username='$user'");
  
       $password = $row = mysql_fetch_array($result);
       echo $password;

// At this stage you can do anything with the $password
?>
</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.