Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP操作MySQL数据库及Redis统计

PHP操作MySQL数据库及Redis统计

墨辰丷
墨辰丷asal
2018-06-06 09:34:571327semak imbas

本篇文章主要介绍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冒泡排序与快速排序算法

Atas ialah kandungan terperinci PHP操作MySQL数据库及Redis统计. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn