web_master Posted October 25, 2009 Share Posted October 25, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/178934-solved-distinct/ Share on other sites More sharing options...
HaLo2FrEeEk Posted October 25, 2009 Share Posted October 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/178934-solved-distinct/#findComment-944029 Share on other sites More sharing options...
Mchl Posted October 25, 2009 Share Posted October 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/178934-solved-distinct/#findComment-944030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.