mAngel Posted June 2, 2010 Share Posted June 2, 2010 Hi, have got a problem with adding uniqe records to m my table here is structure: CREATE TABLE IF NOT EXISTS `ranges` ( `stroid` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `ip` varchar(15) NOT NULL, `code` varchar(2) NOT NULL, `ripe` varchar(255) NOT NULL, PRIMARY KEY (`stroid`), UNIQUE KEY `ripe` (`ripe`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ; in ripe I want to keep ip ranges like "127.0.0.1 - 127.0.0.2" so I set it to uniqe but: 1)When I've run mysql to check if it's allready inside db how to figure if it is or not. 2)how to optimize the code 3)first I use DISTRICT to get no double lines from mysql but I think no good to add double to mysql so rest of my code $dane = file("dane.txt"); $ilosc_rang = count($dane); $test = file_exists("dane.txt"); if (!$test) { echo "no file on server" .$dane; } else { for ($i=0; $i <$ilosc_rang; $i++) { //echo $dane[$i]."<br />"; } if ($ilosc_rang == 0) { echo("file is blank"); } for ($i=0 ; $i<$ilosc_rang; $i++) { $linia[$i] = explode (" - ",$dane[$i]); } echo ("how many ranges:".$ilosc_rang)."<br />"; } for ($i=0; $i<$ilosc_rang; $i++) { $ip=$linia[$i][0]; $idcheck = $DB->fetch_row("SELECT stroid FROM ranges WHERE ip='" . $ip . "'"); if(!empty($idcheck['stroid']) && empty($stroid)) { echo '<p>This range is allready in database</p>'; exit; } else{ $netname="just test"; $countrinf-"cc"; $ripe="127.0.0.1"; $DB->query($sql = "INSERT INTO `test`.`ranges` (`stroid`, `name`, `ip`, `code`, `ripe`) VALUES (NULL, '$netname', '$ip', '$countryinfo', '$ripe');"); } } } if anyone have got solution... Thx in advance Quote Link to comment https://forums.phpfreaks.com/topic/203609-uniqe-records-to-mysql/ Share on other sites More sharing options...
Mchl Posted June 2, 2010 Share Posted June 2, 2010 Why would you keep IP ranges in VARCHAR? Use two INT fields and INET_ATON() and INET_NTOA() funcitons. Quote Link to comment https://forums.phpfreaks.com/topic/203609-uniqe-records-to-mysql/#findComment-1066529 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.