Home > Backend Development > PHP Tutorial > PHP operates MySQL database and Redis statistics

PHP operates MySQL database and Redis statistics

墨辰丷
Release: 2023-03-29 21:54:02
Original
1380 people have browsed it

This article mainly introduces PHP operation of MySQL database and Redis statistics. Interested friends can refer to it. I hope it will be helpful to everyone.

The details are as follows:

<?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);
*/
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP calls the automatic reply interface based on curl

php WeChat red envelope sending algorithm

php bubble sort and quick sort algorithm

The above is the detailed content of PHP operates MySQL database and Redis statistics. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template