Jump to content

[SOLVED] Distinct


web_master

Recommended Posts

Hi,

 

I made an shoutbox, I want to ban some IP-s, and after that list the banned IP-s but, if there is a more than one of a same IP, I want to list one of each IP. My code is looks like this:

 

<?php 
// Reload from dBase
$QueryReturn = mysql_query ( 'SELECT DISTINCT
	`shout_id`,
	`shout_ip`,
	`shout_ban`,
	`shout_ban_datetime`
FROM
	`shout`
WHERE `shout_ban` = "1" 
ORDER BY `shout_id` DESC
' );

// Check query
if ( !$QueryReturn ) { echo mysql_error(); exit; }

// Request query
while ( $REQUEST = mysql_fetch_array ( $QueryReturn ) ) {

echo $REQUEST[ 'shout_ip' ];
}
?>


 

what am I wrong, because that list everything not only one IP...

Link to comment
Share on other sites

So basically if there's duplicate IP's you only want to print one of each?  You could do that like this:

 

while($REQUEST = mysql_fetch_assoc($QueryReturn)) {
  $ip[] = $_REQUEST['shout_ip'];
  }
$ips = array_unique($ip);
foreach($ips as $ip) {
  echo $ip;
  }

 

It's a little more code, but basically what you're doing is creating a new array with just the ips in it, then you run it through array_unique() to remove duplicate entries, then you loop through it and print each value.

 

EDIT: There is another way, I think.  Just change your query to this:

 

$QueryReturn = mysql_query ( 'SELECT DISTINCT
	`shout_id`,
	`shout_ip`,
	`shout_ban`,
	`shout_ban_datetime`
FROM
	`shout`
WHERE `shout_ban` = "1" 
ORDER BY `shout_id`
GROUP BY `shout_ip` DESC
' );

 

I'm not 100% sure, but I think that will only pull 1 of each unique value from the database.

Link to comment
Share on other sites

DISTINCT ensures your query returns only unique rows (no two rows are the same), but it applies to all columns in a query not just to the first one.

To select only one of each IP, you might want to GROUP BY shout_ip

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.