Home » , » Remove duplicate entries from php mysql database

Remove duplicate entries from php mysql database

Written By Unknown on Sabtu, 16 Juli 2011 | 02.47

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);
}

Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger