To remove duplicate entries from php-mysql database based on a fieldname use this code. This example will remove all duplicate entries of email in the table emails.
$query="SELECT DISTINCT email FROM emails
GROUP BY email HAVING count(*)>1";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
$sql="SELECT id FROM emails WHERE
email='".$row['email']."' ORDER BY id";
$res=mysql_query($sql);
$rowd=mysql_fetch_array($res);
$count = @mysql_num_rows($res) - 1;
$delsql="DELETE FROM emails WHERE
id='".$rowd['id']."' LIMIT ".$count;
mysql_query($delsql);
}
Posting Komentar