herghost Posted July 7, 2010 Share Posted July 7, 2010 Hi all, I am working on a signup for which checks the database for a username and displays a message using ajax if the name is available or not. I have the username dave in the database, yet when I type dave into the box it still shows as available? <?php include('connect.php'); $query = "SELECT * FROM user_signup where username = '{$_POST['username']}'"; $result = mysql_query($query); if(mysql_num_rows($result)>0){ //username already exists echo "yes"; }else{ echo "no"; } ?> Link to comment https://forums.phpfreaks.com/topic/207055-help-with-pulling-usernames-from-database/ Share on other sites More sharing options...
marcus Posted July 7, 2010 Share Posted July 7, 2010 $sql = "SELECT * FROM `user_signup` WHERE `username`='".mysql_real_escape_string($_POST['username'])."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) > 0){ // taken }else { // available } Link to comment https://forums.phpfreaks.com/topic/207055-help-with-pulling-usernames-from-database/#findComment-1082681 Share on other sites More sharing options...
Pikachu2000 Posted July 7, 2010 Share Posted July 7, 2010 What do you mean "shows it as available"? If you mean when you type a username in that is already in the database, it echos back "yes", then it's doing exactly what it's written to do. Additionally, a SELECT COUNT(`primary_key_field`) query rather than mysql_num_rows() on a wildcard SELECT *, would be much more efficient in that application. Link to comment https://forums.phpfreaks.com/topic/207055-help-with-pulling-usernames-from-database/#findComment-1082684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.