AKalair Posted September 15, 2008 Share Posted September 15, 2008 Hey guys, I'm making a real basic login that looks up the user name entered in a database and if it exists says Welcome. But the query isn't actually running, I've printed the output of it to see what's going out and its just returning what I've set the variable to i.e. SELECT ... <? mysql_connect( "localhost", ); $dbcnx = @mysql_connect( "localhost", ); if (!$dbcnx) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit(); } $user = "SELECT username FROM users WHERE username = '$_POST[username]'"; echo "$user"; if ($user == '1') { echo "Logged in Welcome"; } else { echo "Invalid Sorry"; } ?> Returns SELECT username FROM users WHERE username = 'test'Invalid Sorry Any ideas, thanks. PS. Ive removed my username and password from the top but it does connect. Quote Link to comment https://forums.phpfreaks.com/topic/124334-solved-php-sql-query-not-querying/ Share on other sites More sharing options...
KevinM1 Posted September 15, 2008 Share Posted September 15, 2008 Hey guys, I'm making a real basic login that looks up the user name entered in a database and if it exists says Welcome. But the query isn't actually running, I've printed the output of it to see what's going out and its just returning what I've set the variable to i.e. SELECT ... <? mysql_connect( "localhost", ); $dbcnx = @mysql_connect( "localhost", ); if (!$dbcnx) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit(); } $user = "SELECT username FROM users WHERE username = '$_POST[username]'"; echo "$user"; if ($user == '1') { echo "Logged in Welcome"; } else { echo "Invalid Sorry"; } ?> Returns SELECT username FROM users WHERE username = 'test'Invalid Sorry Any ideas, thanks. PS. Ive removed my username and password from the top but it does connect. You need to actually execute the query and fetch the results: $query = "SELECT username FROM users WHERE username = '{$_POST['username']}'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $user = $row['username']; Keep in mind, unless the username in the DB is actually 1, your test will fail every time. Quote Link to comment https://forums.phpfreaks.com/topic/124334-solved-php-sql-query-not-querying/#findComment-642075 Share on other sites More sharing options...
AKalair Posted September 15, 2008 Author Share Posted September 15, 2008 <? mysql_connect( "localhost", ); $dbcnx = @mysql_connect( "localhost", ); if (!$dbcnx) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit(); } $query = "SELECT username FROM users WHERE username = '{$_POST['username']}'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); $user = $row['username']; if ($user == 'true') { echo "Logged in Welcome"; } else { echo "Invalid Sorry"; } ?> Returns Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/thegamer/public_html/PHP/login2.php on line 14 Invalid Sorry Does this mean I've spelt a database name wrong or something? Quote Link to comment https://forums.phpfreaks.com/topic/124334-solved-php-sql-query-not-querying/#findComment-642086 Share on other sites More sharing options...
wildteen88 Posted September 15, 2008 Share Posted September 15, 2008 no it means there is a problem with the result resource returned from mysql_query. This normally means you have an error in your query. Whenever you come across such an error always add 'or die(mysql_error())' after your call to mysql_query eg $result = mysql_query($query) or die(mysql_error()); If there is a problem with your query an error message from MySQL will be displayed. Quote Link to comment https://forums.phpfreaks.com/topic/124334-solved-php-sql-query-not-querying/#findComment-642098 Share on other sites More sharing options...
AKalair Posted September 15, 2008 Author Share Posted September 15, 2008 Ok ive fixed it. Is there a way to make the query return 1 or something if its found so I can use an If statement its not much use if I have to have an if statement for every user name. Quote Link to comment https://forums.phpfreaks.com/topic/124334-solved-php-sql-query-not-querying/#findComment-642111 Share on other sites More sharing options...
wildteen88 Posted September 15, 2008 Share Posted September 15, 2008 You can check to see if the query returned any rows (results) using mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/124334-solved-php-sql-query-not-querying/#findComment-642168 Share on other sites More sharing options...
AKalair Posted September 16, 2008 Author Share Posted September 16, 2008 Sorted now thanks guys I really appreciate it Quote Link to comment https://forums.phpfreaks.com/topic/124334-solved-php-sql-query-not-querying/#findComment-642649 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.