Page 1 of 1

[SOLVED] Finding out if a row exists

Posted: Mon Nov 17, 2003 6:25 am
by starsol
I'm creating my first membership script with PHP (using a MySQL database) and I've stubled across a smal problem and can't find the answer anywhere (although I'm sure it's pretty simple).

If a member wants a username (which is set to be unique) that is the same as a current member, the following error gets returned:

Error: Duplicate entry 'desiredusername' for key 1

How can I check if there is already a row in a table with desiredusername as a username before trying to create the row? A function returning TRUE or FALSE is what I'm really looking for.

Thanks in advance,
Rupe

Posted: Mon Nov 17, 2003 6:30 am
by JayBird
what about this

Code: Select all

if (mysql_num_rows($result) == 0) {
   // Add the user
} else {
   // Usermame already exists
}
have a look at [php_man]mysql_num_rows[/php_man] in the manual

Mark

Posted: Mon Nov 17, 2003 6:31 am
by starsol
Bech100 wrote:what about this

Code: Select all

if (mysql_num_rows($result) == 0) {
   // Add the user
} else {
   // Usermame already exists
}
have a look at mysql_num_rows in the manual

Mark

Cheers, can't believe I missed that :o.