Duplicate key check
am 19.05.2006 23:50:17 von KoRnDragonI'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.
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.
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
Done. Now how do I use checking for the duplicate key to display the
error?
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