Duplicate key check

Duplicate key check

am 19.05.2006 23:50:17 von KoRnDragon

I'm making a "plug your site" script and I need to check my DB for
duplicate URLs before adding a new entry.

I'm new to SQL so the more help the better.

Thanks in advance.

Re: Duplicate key check

am 20.05.2006 01:53:18 von nc

korndragon@gmail.com wrote:
>
> I'm making a "plug your site" script and I need to check my DB for
> duplicate URLs before adding a new entry.

You don't. Make URL a unique-index field and add keyword IGNORE after
INSERT. This will ensure that no records with the same URL are
created. Alternatively, create an additional field and populate it
with MD5 hash of the URL and index that field rather than the URL.

Cheers,
NC

Re: Duplicate key check

am 20.05.2006 16:08:33 von KoRnDragon

Done. Now how do I use checking for the duplicate key to display the
error?

Re: Duplicate key check

am 20.05.2006 18:23:24 von nc

korndragon@gmail.com wrote:
>
> Done. Now how do I use checking for the duplicate key
> to display the error?

Run the INSERT IGNORE query and then call mysql_affected_rows(). If it
returns 1, the addition has taken place. If it returns 0, the record
was not added and therefore was duplicate...

Cheers,
NC