Mysql Q.

Discussion in 'PHP' started by Alice24, Aug 13, 2009.

  1. #1
    It is possible to find if there are 2 ip's the same in a database but without have one to search for? i mean let's say i have 100 ip in my database.. how can i querry the database to see if there are 2 same ip's?
     
    Alice24, Aug 13, 2009 IP
  2. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Something like this.

    SELECT ip, COUNT(*) AS countnum FROM tablename GROUP BY ip;

    Basically you can then loop thru the results and find ones with a 'countnum' field as higher than 1 (as I'm not sure throwing 'WHERE countnum > 1' would work).
     
    kblessinggr, Aug 13, 2009 IP
  3. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Actually, I think it should be HAVING countnum > 1.
     
    premiumscripts, Aug 13, 2009 IP
  4. Alice24

    Alice24 Greenhorn

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #4
    I can see the numbers of distinct ip's

    
    <?php
    
    $query = "SELECT COUNT(DISTINCT ip_address)  FROM ip"; 
    	 
    $result = mysql_query($query) or die(mysql_error());
    
    
    while($ro = mysql_fetch_array($result)){
    	
    
      $bo=$ro['COUNT(DISTINCT ip_address)'];
    echo "$bo";
    
    }
    
    ?>
    
    PHP:
    and with a little more code i can see that uniq ip's
    
    
    $resulte = mysql_query("SELECT ip_address, COUNT(*)  FROM ip GROUP BY ip_address") 
    or die(mysql_error());  
    
    echo "<table border='1'>";
    echo "<tr> <th>Number</th> <th>Ip</th> </tr>";
    // keeps getting the next row until there are no more to get
    while($row = mysql_fetch_array( $resulte )) {
    	// Print out the contents of each row into a table
    	echo "<tr><td>"; 
    	echo $row['COUNT(*)'];
    	echo "</td><td>"; 
    	echo $row['ip_address'];
    	echo "</td></tr>"; 
    } 
    
    echo "</table>";
    
    
    PHP:
    EDIT.

    Works. :D Thank you.
     
    Last edited: Aug 13, 2009
    Alice24, Aug 13, 2009 IP