php - Duplicate detection code not working -


i have simple piece of code here, add bunch of links in database, check each link 200 ok.

<?php function check_alive($url, $timeout = 10) {       $ch = curl_init($url);       // set request options       curl_setopt_array($ch, array(         curlopt_followlocation => true,         curlopt_nobody => true,         curlopt_timeout => $timeout,         curlopt_useragent => "page-check/1.0"        ));       // execute request       curl_exec($ch);       // check if error occurred       if(curl_errno($ch)) {         curl_close($ch);         return false;       }       // http response code       $code = curl_getinfo($ch, curlinfo_http_code);       curl_close($ch);       // page alive if 200 ok received       return $code === 200;  }  if (isset($_get['cron'])) {     // database connection     $c = mysqli_connect("localhost", "paydayci_gsa", "", "paydayci_gsa");        //$files = scandir('links/');     $files = glob("links/*.{*}", glob_brace);     foreach($files $file)      {         $json = file_get_contents($file);         $data = json_decode($json, true);                if(!is_array($data)) continue;         foreach ($data $platform => $urls)         {                            foreach($urls $link)             {                 //echo $link;                 $lnk = parse_url($link);                 $resunique = $c->query("select * `links_to_check` `link_url` '%".$lnk['host']."%'");                 // if no duplicate insert in database                 if(!$resunique->num_rows)                 {                     $i = $c->query("insert `links_to_check` (link_id,link_url,link_platform) values ('','".$link."','".$platform."')");                  }             }         }         // @ end delete file         unlink($file);     }     // check if urls alive     $select = $c->query("select * `links_to_check` order `link_id` asc");     while($row = $select->fetch_array()){            $alive = check_alive($row['link_url']);         $live = "";         if ($alive == true)          {             $live = "y";             $lnk = parse_url($row['link_url']);             // check duplicate             $resunique = $c->query("select * `links` `link_url` '%".$row['link_url']."%'");             echo $resunique;             // if no duplicate insert in database             if(!$resunique->num_rows)             {                 $i = $c->query("insert links (link_id,link_url,link_platform,link_active,link_date) values ('','".$row['link_url']."','".$row['link_platform']."','".$live."',now())");             }                }            $c->query("delete `links_to_check` link_id = '".$row['link_id']."'");     } }  ?> 

i'm trying not add duplicate urls database still getting in, have missed obvious code can see? have looked on few times, can't see staring out @ me.

if trying enforce unique values in database, should relying on database enforce constraint. can add index (assuming using mysql or variant, syntax appears be) this:

alter table `links` add unique index `idx_link_url` (`link_url`); 

one thing aware of spaces prefixes/suffixes use trim() on values , also, should strip trailing slashes keep consistent (so don't dupes) using rtrim().


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -