Best way to avoid duplicate entry into mysql database

后端 未结 4 1102
孤街浪徒
孤街浪徒 2020-11-22 09:53

I have a table with 3 columns - id (pk), pageId (fk), name. I have a php script which dumps about 5000 records into the table, with about half being duplicates, with same pa

4条回答
  •  失恋的感觉
    2020-11-22 10:42

    You can set the PageID and Name to a Unique index in the MySQL database. This way when you insert the rows, it will cause an error, which can be ignored by PHP, and you can just go to the next row.

    This assumes you are inserting rows individually. AKA:

    foreach($large_data as $fields)
    {
        mysql_query("INSERT INTO TABLE (`Something`) VALUES('".$fields['something']."');
    }
    

提交回复
热议问题