首页  >  文章  >  后端开发  >  PHP操作MySQL数据库及Redis统计

PHP操作MySQL数据库及Redis统计

墨辰丷
墨辰丷原创
2018-06-06 09:34:571325浏览

本篇文章主要介绍PHP操作MySQL数据库及Redis统计,感兴趣的朋友参考下,希望对大家有所帮助。

具体如下:

<?php
  header("Content-Type:text/html;charset=utf-8");
  include &#39;lib/mysql.class.php&#39;;
  $mysql_obj = mysql::getConn();
  //redis 
  $redis = new Redis();
  $redis->pconnect(&#39;127.0.0.1&#39;, 6379);
  if(isset($_SERVER[&#39;HTTP_REFERER&#39;])){
    $url_md5 = md5($_SERVER[&#39;HTTP_REFERER&#39;]);
  }
  $adve_key = &#39;adve&#39;; 
  $adve_key_exists = &#39;adve_exists&#39;;
  if(!$redis->exists($adve_key_exists)){
    $list = $mysql_obj->fetch_array("select * from user_online_adve");
    if($list){
      foreach ($list as $key => $value) {
        $url_hash = md5($value[&#39;adve_url&#39;]);
        $adve_hash_key = $adve_key.":".$url_hash;
        $id = $value[&#39;id&#39;];
        $redis->set($adve_hash_key,$id);
        $redis->set($adve_key_exists,true);
        //$redis->hmset($adve_hash_key, array(&#39;id&#39; =>$id));
        //print_r($redis->get($adve_hash_key));
      }
    }
  }
  $adve_new_key = $adve_key.&#39;:&#39;.$url_md5;
  if($redis->exists($adve_new_key)){
      $adve_plus = $adve_new_key.":plus" ;
      if(!$redis->exists($adve_plus)){
        $redis->set($adve_plus,1); 
      }else{
        $redis->incr($adve_plus);
        $num = $redis->get($adve_plus);
        if($num >10){
          $id = $redis->get($adve_new_key);
          // insert to sql;
          $mysql_obj->query("update user_online_adve set adve_num=adve_num+$num where id=$id");
          $redis->set($adve_plus,1);
        }
      }
  }
  header(&#39;HTTP/1.0 301 Moved Permanently&#39;);
  header(&#39;Location: https://itunes.apple.com/cn/app/san-guo-zhi15-ba-wangno-da-lu/id694974270?mt=8&#39;);
/*
  if(){
      $adve_plus = $adve_key.":plus" ;
      if($redis->exists($adve_plus)){
        $redis->incr($adve_plus);
      }else{
        $redis->set($adve_plus,1); 
      }
      echo $redis->get($adve_plus);
  }
  foreach ($list as $key => $value) {
      $url_hash = md5($value[&#39;adve_url&#39;]);
      $id = $value[&#39;id&#39;];
      $adve_num = $value[&#39;adve_num&#39;];
      $adve_plus = $adve_key.":plus" ;
      if($redis->exists($adve_plus)){
        $redis->incr($adve_plus);
      }else{
        $redis->set($adve_plus,1); 
      }
      echo $redis->get($adve_plus);
      //if($redis->)
      //$redis->hmset($adve_key, array(&#39;id&#39; =>$id, &#39;adve_num&#39;=>$adve_num));
      //print_r($redis->hmget("adve:$url_hash", array(&#39;adve_num&#39;)));
  }
    print_r($list);
*/

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP基于curl调用自动回复接口

php微信发红包算法

php冒泡排序与快速排序算法

以上是PHP操作MySQL数据库及Redis统计的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn