Uphoreum Posted July 24, 2006 Share Posted July 24, 2006 Say each user had a row in a table to store information about them and I want the row to automatically be created if it's not there, but I don't want a new one to be created with the same username. So, one column would be for the username and I would create one if no rows had that username. Is there a mysql query or something for this, or do I have to cycle through each row and check if it has that username manually? Quote Link to comment https://forums.phpfreaks.com/topic/15443-test-for-row-already-created/ Share on other sites More sharing options...
Moon-Man.net Posted July 24, 2006 Share Posted July 24, 2006 in the Database its self, you could set the field that you want to be unique to be a "Unique restraint" field...If you try and insert into the database a value that exists in that field, you will get a SQL error...Cheers,Nathan Quote Link to comment https://forums.phpfreaks.com/topic/15443-test-for-row-already-created/#findComment-62622 Share on other sites More sharing options...
spyke01 Posted July 24, 2006 Share Posted July 24, 2006 you can also do this[code]$sql = "SELECT * FROM `users` WHERE users_username='$requestedUsername'";$result = mysql_query($sql);if (mysql_num_rows($result) == 0) { // username is not in the databse so continue creating the account}else { // username exists so tell the user so they can choose another one}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15443-test-for-row-already-created/#findComment-62679 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.