That's what I first tried. I must have mis-coded as I kept getting a positive number on mysql_num_rows when the name was unique.

I will take a look at the url you suggested. Thanks.

grNadpa

RichardAskew
—
2012-10-05T15:59:10Z —
#6

You will ger a positive value if the name does exist in the database.

Grnadpa said:

That's what I first tried. I must have mis-coded as I kept getting a positive number on mysql_num_rows when the name was unique.

I will take a look at the url you suggested. Thanks.

grNadpa

guido2004
—
2012-10-05T16:03:45Z —
#7

Grnadpa said:

(as 'name' is NOT the key to this table).

Depending on what you want to do, you could also create a unique index on 'name', do an insert, and then handle the duplicate key error if the name already exists.

r937
—
2012-10-05T16:04:41Z —
#8

okay, to prevent duplicate names, declare a UNIQUE index on the name column

then, don't bother with this function at all -- just go ahead and do the INSERT without checking yourself

to handle what happens if a name already exists, you have several choices --

you can use the INSERT IGNORE option, which lets the INSERT statement finish normally, but does not insert anything

you can use the INSERT ON DUPLICATE KEY UPDATE option, to capture any additional data values from the insert and use them to update the existing row

you can catch the error and report back a user-friendly message like "Name already exists"

Grnadpa
—
2012-10-05T17:56:42Z —
#9

guido2004 said:

Depending on what you want to do, you could also create a unique index on 'name', do an insert, and then handle the duplicate key error if the name already exists.

r937 said:

okay, to prevent duplicate names, declare a UNIQUE index on the name column

then, don't bother with this function at all -- just go ahead and do the INSERT without checking yourself

...

3. you can catch the error and report back a user-friendly message like "Name already exists"

Thanks guys. That sounds like the better approach. I have my validation packaged in a separate function within my class than the database maintenance (Insert/Update). As such, got into a bit of tunnel vision.