droidus Posted July 28, 2011 Share Posted July 28, 2011 i am having issues checking the availability of a username. here is my code: <?php $loginUsername = "admin"; mysql_select_db($database_uploader, $uploader); $query = "SELECT * FROM members WHERE uname='" . mysql_real_escape_string($loginUsername) . "'"; // make sure the username and password were found if (mysql_num_rows($result) > 0) { echo "This username is taken. Please choose a different one."; } else { echo "This username is available."; } mysql_close($con); ?> the username, admin, does exist. but i get an output of "This username is available. ". why? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/243047-check-username/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 28, 2011 Share Posted July 28, 2011 You are not executing your query, so your logic always reports that the username is available. You should also be developing and debugging your code on a system with error_reporting set to E_ALL (or even better a -1) and display_errors set to ON so that all the php detected errors will be reported and displayed, you will save a TON of time. Quote Link to comment https://forums.phpfreaks.com/topic/243047-check-username/#findComment-1248280 Share on other sites More sharing options...
AyKay47 Posted July 28, 2011 Share Posted July 28, 2011 i don't recommend inserting a function in your query like that... <?php $loginUsername = "admin"; mysql_select_db($database_uploader, $uploader); $query = sprintf("SELECT * FROM members WHERE uname='%s'", mysql_real_escape_string($loginUsername)); mysql_query($query); // make sure the username and password were found if (mysql_num_rows($result) > 0) { echo "This username is taken. Please choose a different one."; } else { echo "This username is available."; } mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/243047-check-username/#findComment-1248281 Share on other sites More sharing options...
droidus Posted July 28, 2011 Author Share Posted July 28, 2011 ok, then what would you recommend? Quote Link to comment https://forums.phpfreaks.com/topic/243047-check-username/#findComment-1248450 Share on other sites More sharing options...
Nodral Posted July 28, 2011 Share Posted July 28, 2011 Do all your functions and processing, etc outside of your query statement, then insert it as a variable. e.g. $loginUsername = "admin"; $loginUsername=mysql_real_escape_string($loginUsername); mysql_select_db($database_uploader, $uploader); $query = sprintf("SELECT * FROM members WHERE uname='%s'", $loginUsername); mysql_query($query); Quote Link to comment https://forums.phpfreaks.com/topic/243047-check-username/#findComment-1248493 Share on other sites More sharing options...
AyKay47 Posted July 28, 2011 Share Posted July 28, 2011 ok, then what would you recommend? i recommend you do something like I posted..Nodral did something similar as well Quote Link to comment https://forums.phpfreaks.com/topic/243047-check-username/#findComment-1248498 Share on other sites More sharing options...
Nodral Posted July 28, 2011 Share Posted July 28, 2011 Possibly want to use mysql_query($query) or die("<b>A fatal MySQL error occured</b>.\n<br />Query: " . $query . "<br />\nError: (" . mysql_errno() . ") " . mysql_error()); Then if you have got issues, it will tell you. Quote Link to comment https://forums.phpfreaks.com/topic/243047-check-username/#findComment-1248503 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.