PHP数据采集写入数据库

 setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
     }catch(PDOException $e){
         echo"数据库连接失败".$e->getMessage();
     }
     //设置时区
     date_default_timezone_set("PRC");
     //采集地址
     $url = 'xxxxxx';
     $str = file_get_contents($url);
     $arr = json_decode($str,true);
     foreach($arr as $val){
         //组装数据入库,例如:
         $title = $val['title'];
         $content= $val['content'];
         $addtime = $val['addtime'];
         //ignore  忽略主键与唯一ID重复的元素,不出错
         $sql1 = "INSERT ignore INTO `gao`( `id`, `title`,  `content`,  `addtime`) VALUES (null,'".$title."','".$content."','".$addtime."')";
         $stmt1 = $pdo->prepare($sql1);
         $res = $stmt1->execute();
     }
     if($res){
         die('success');
      }else{
         exit('fail');
    }

你可能感兴趣的:(php)