Jump to content


Photo

Test For Row Already Created


  • Please log in to reply
2 replies to this topic

#1 Uphoreum

Uphoreum
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 24 July 2006 - 02:28 AM

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?

#2 Moon-Man.net

Moon-Man.net
  • Members
  • PipPipPip
  • Advanced Member
  • 84 posts

Posted 24 July 2006 - 02:38 AM

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

#3 spyke01

spyke01
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 24 July 2006 - 07:12 AM

you can also do this
$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
}


[a href="http://www.fasttracksites.com" target="_blank"]Cheap PHP Hosting[/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users