zamp0e Posted June 7, 2009 Share Posted June 7, 2009 I would like to check if the username the user wants when he registers already exists in the database, and if so, return a error message. Is there a special funktion or something like that to do this ? Link to comment https://forums.phpfreaks.com/topic/161294-check-if-username-exists-in-database/ Share on other sites More sharing options...
fenway Posted June 7, 2009 Share Posted June 7, 2009 No function -- just query the users table with JUST the user name, then in php check the hashed password against the value you've stored in your DB. Link to comment https://forums.phpfreaks.com/topic/161294-check-if-username-exists-in-database/#findComment-851160 Share on other sites More sharing options...
cunoodle2 Posted June 8, 2009 Share Posted June 8, 2009 Here is some code that I wrote in a recent page checking to see if an email address already existed. You can just modify to use that of username. Note that this is written using pdo statements which are a little more challenging but also way more secure. At the very least it will give you the structure of how you should approach this.. <?php //prepare SQL query using pdo statement $stmt = $connect->prepare("SELECT `EmailAddy` FROM `Members` where EmailAddy = ? LIMIT 1;"); if ($stmt->execute(array($email))) { $result = $stmt->fetch(PDO::FETCH_ASSOC); //check to see if there is already that email address in the database if((strtolower($email) == strtolower($result["EmailAddy"]))) { $errorMessage = "Error: That email address already exists.<br />\n"; } } ?> Link to comment https://forums.phpfreaks.com/topic/161294-check-if-username-exists-in-database/#findComment-851846 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.