Jump to content

SQL check duplicates


karimali831

Recommended Posts

I've actually been needing something like this for a site I'm working on, so I sat down and figured it out. The table returns the duplicate value in the field, and the total number of entries the value has. I have it in a function, but this is the guts of it, with an echo() instead of return().

 

$field = 'your_field';
$table = 'your_table';
require_once("your_db_conn_script");
$output = '';
$query = "SELECT `{$field}`, COUNT(`{$field}`) AS c FROM `{$table}` GROUP BY `{$field}` ORDER BY c DESC";
$result = mysql_query( $query );
$output = "<table>";

while( $array = mysql_fetch_assoc($result) ) {
if( $array['c'] > 1 ) {
$bg != "CCCCCC" ? $bg = "CCCCCC" : $bg = "FFFFFF";
$count = $array['c'];
$output .=  "<tr bgcolor=\"$bg\"><td>Value:</td><td>" . $array['ident'] . "</td><td>Entries:</td><td>" . $count . "</td></tr>";
}
}
$output .= "</table>";
echo $output;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.