View Single Post
  #1 (permalink)  
Old 01-12-04, 05:20 PM
seans9 seans9 is offline
Newbie Coder
 
Join Date: Sep 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
delete duplicate rows in mysql using php

ok im not that advanced in php...but here is the deal. i have a database with a lot of email addresses (some of which are duplicates). each email address has its own row. what i want to do is go through and delete the rows with duplicate ones (leaving at least one of the email addresses)...i hope that makes sense. this is what i wrote and its not working. PLEASE help. thanks

PHP Code:

$query="SELECT * FROM `emails` ORDER BY `email` ASC LIMIT 0, 30";

mysql_query($query);
$result=mysql_query($query); 

$numrows=mysql_num_rows($result);

$totalemailsdeleted=0;
$i=1;
$i2=0;
while(
$i2 $numrows)
{
    
$id=mysql_result($result$i2id);
    
$email=mysql_result($result$i2email);
    while(
$i $numrows)
    {
        
$idcompare=mysql_result($result$iid);
        
$emailcompare=mysql_result($result$iemail);
        if(
$emailcompare == $email)
        {
            
$query="DELETE FROM `emails` WHERE `id` = '$idcompare' LIMIT 1";
            
mysql_query($query);
            
$emailsdeleted.="$emailcompare<br>";
            
$totalemailsdeleted++;
            
$numrows=mysql_num_rows($result);
        }
        else
        {
        }
        
$i++;
    }
    
$i2++;

Reply With Quote