ホームページ  >  記事  >  バックエンド開発  >  PHP は MySQL データベースと Redis 統計を操作します

PHP は MySQL データベースと Redis 統計を操作します

墨辰丷
墨辰丷オリジナル
2018-06-06 09:34:571325ブラウズ

この記事では主に MySQL データベースの PHP 操作と 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 WeChat 赤封筒アルゴリズム

php バブルソートおよびクイックソートアルゴリズム

以上がPHP は MySQL データベースと Redis 統計を操作しますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。