Ignore Duplicate Entries - PHP

If this were a perfect world, all data received would never have a duplicate key. It obviously isn't a perfect world since MySQL created this little function (IGNORE). Lets say i get a data dump and in it it has these 2 rows: 1 2 productname product description 3 1 2 productname product description (sub description) 3

They are not identical but the primary keys are. Since primary keys have to be unique this throws an error.

Here is how we get around this error:

$sql = "INSERT IGNORE INTO ".$table." (".$column.") VALUES (".$value.")";

Ths will drop the second line and continue on with the script.

$sql = "INSERT INTO ".$table." (".$column.") VALUES (".$value.")"
  ON DUPLICATE KEY UPDATE ".$column[0]."=".$column[0]+1"";

This will update the previous row. Most of the time you do not want to change a primary key if the data is dumped to you. So i cannot stress to you enough to emphasize to your datafeed distributer to give you nice, clean dumps!!!

 
development/sql/ignore_duplicates.txt · Last modified: 2007/09/14 14:42 by vincenzobar
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki