atmega-ist Posted May 31, 2010 Share Posted May 31, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/203389-select-statement-not-working/ Share on other sites More sharing options...
riwan Posted May 31, 2010 Share Posted May 31, 2010 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"]; Quote Link to comment https://forums.phpfreaks.com/topic/203389-select-statement-not-working/#findComment-1065527 Share on other sites More sharing options...
atmega-ist Posted May 31, 2010 Author Share Posted May 31, 2010 *sigh* ... this is what visual studio's intellisense does to people... thanks for the suggestion - it works perfectly! Quote Link to comment https://forums.phpfreaks.com/topic/203389-select-statement-not-working/#findComment-1065531 Share on other sites More sharing options...
david.brown Posted May 31, 2010 Share Posted May 31, 2010 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> Quote Link to comment https://forums.phpfreaks.com/topic/203389-select-statement-not-working/#findComment-1065540 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.