Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-18-2009, 08:43 PM   PM User | #1
seco
Regular Coder

 
seco's Avatar
 
Join Date: Nov 2008
Location: Oregon
Posts: 682
Thanks: 5
Thanked 79 Times in 77 Posts
seco has a little shameless behaviour in the past
Find all duplicates in a mysql field

If you are ever in need of finding duplicates in a mysql field but dont want to open phpmyadmin you can simply do this.

PHP Code:
<?php
    mysql_connect
("localhost""username""password") or die(mysql_error());
    
mysql_select_db("dbname") or die(mysql_error());

    
$result mysql_query("select field, count(field) as cnt
        from table
        group by field
        having cnt > 1
        order by cnt; "

    or die(
mysql_error());
     
     while(
$row mysql_fetch_array$result )) 
     
      {
        echo 
$row['field']."<br>";
      }
?>
This will return any duplicates, but only 1 result of the dup.

Of course change table and field in the snippet to your own.
seco is offline   Reply With Quote
Reply

Bookmarks

Tags
duplicates, mysql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:28 AM.


Advertisement
Log in to turn off these ads.