Sunday, February 17, 2008

What Not to Do on Duplicate Records

You've probably grabbed a bunch of records from the database, pumped them into an array and then tried to delete the duplicate records. The result is a dataset that is messed up, the reason why is unknown.

Lets look at the array:

// Your query here
$SqlSelectRow = mysql_fetch_array($SqlSelectResult)

This behavior occurs because you perform the action on the contents in the array. Updates to the array are not reflected on the database, and therein lies the problem.

Be sure to directly update records in the database or have the array update contents in the db.

An easy solution though is to set the column as 'unique' and then dump the dataset. Be sure to remove the unique attribute once this process completes if you want duplicate records during future inserts.

No comments: