Duplicate key check
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
korndragon [at] 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
Done. Now how do I use checking for the duplicate key to display the
error?
Re: Duplicate key check
korndragon [at] 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