How to check if a value already exists to avoid duplicates?

前端 未结 17 1040
小鲜肉
小鲜肉 2020-12-02 23:13

I\'ve got a table of URLs and I don\'t want any duplicate URLs. How do I check to see if a given URL is already in the table using PHP/MySQL?

17条回答
  •  再見小時候
    2020-12-02 23:26

    If you want to insert urls into the table, but only those that don't exist already you can add a UNIQUE contraint on the column and in your INSERT query add IGNORE so that you don't get an error.

    Example: INSERT IGNORE INTO urls SET url = 'url-to-insert'

提交回复
热议问题