Jump to content

Archived

This topic is now archived and is closed to further replies.

Uphoreum

Test For Row Already Created

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.