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
https://forums.phpfreaks.com/topic/178934-solved-distinct/
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
https://forums.phpfreaks.com/topic/178934-solved-distinct/#findComment-944029
Share on other sites

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.