最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PHP+redis實(shí)現(xiàn)添加處理投票的方法

 更新時(shí)間:2015年11月14日 11:13:26   作者:jackluo  
這篇文章主要介紹了PHP+redis實(shí)現(xiàn)添加處理投票的方法,結(jié)合實(shí)例較為詳細(xì)的分析了PHP+redis數(shù)據(jù)庫操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了PHP+redis實(shí)現(xiàn)添加處理投票的方法。分享給大家供大家參考,具體如下:

<?php
 header("Content-Type:text/html;charset=utf-8");
 include 'lib/mysql.class.php';
 $mysql_obj = mysql::getConn();
 if(class_exists('Redis')){
  //redis 
  $redis = new Redis();
  $redis->pconnect('127.0.0.1', 6379);
  if(isset($_SERVER['HTTP_REFERER'])){
   $url_md5 = md5($_SERVER['HTTP_REFERER']);
  }
  $adve_key = 'adve'; 
  $adve_key_exists = 'adve_exists';
  if(!$redis->exists($adve_key_exists)){
   $list = $mysql_obj->fetch_array("select * from admin_online_adve");
   if($list){
    foreach ($list as $key => $value) {
     $url_hash = md5($value['adve_url']);
     $adve_hash_key = $adve_key.":".$url_hash;
     $id = $value['id'];
     $redis->set($adve_hash_key,$id);
     $redis->set($adve_key_exists,true);
    }
   }
  }
  $adve_new_key = $adve_key.':'.$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 >100){
      $id = $redis->get($adve_new_key);
      // insert to sql;
      $mysql_obj->query("update admin_online_adve set adve_num=adve_num+$num where id=$id");
      $redis->set($adve_plus,1);
     }
    }
  }
 }
?>
<html>
<head>
<meta http-equiv="refresh" content="1;url=https://itunes.apple.com/cn/app/san-guo-zhi15-ba-wangno-da-lu/id694974270?mt=8">
<title>統(tǒng)計(jì)</title>
</head>
<body>
 <img src="loading.gif">Loading...
</body>
</html>

其中php連接mysql類mysql.class.php如下:

<?php
define("MYSQL_SQL_GETDATA", 1);
define("MYSQL_SQL_EXECUTE", 2);
class mysql_db{
  var $_server;        //數(shù)據(jù)庫服務(wù)器地址
  var $_user;         //數(shù)據(jù)庫連接帳號(hào)
  var $_password;       //數(shù)據(jù)庫連接密碼
  var $_dbname;        //數(shù)據(jù)庫名稱
  var $_persistency=false;  //是否使用持久連接
  var $_isConnect = false;  //是否已經(jīng)建立數(shù)據(jù)庫連接
  var $_charset="utf8";    //數(shù)據(jù)庫連接字符集
  var $_isDebug = false;   //是否Debug模式
  var $_sql=array();     //執(zhí)行sql語句數(shù)組
  var $_db_connect_id;    //數(shù)據(jù)庫連接對(duì)象標(biāo)識(shí)
  var $_result;        //執(zhí)行查詢返回的值
  var $_record;
  var $_rowset;
  var $_errno = 0;
  var $_error = "connection error";
  var $_checkDB = false;
  function mysql_db($dbserver, $dbuser, $dbpassword,$database,$persistency = false,$autoConnect=false,$checkdb = false)
  {
    $this->_server = $dbserver;
    $this->_user = $dbuser;
    $this->_password = $dbpassword;
    $this->_dbname = $database;
    $this->_persistency = $persistency;
    $this->_autoConnect = $autoConnect;
    $this->_checkDB = $checkdb;
    if($autoConnect){
      $this->connection();
    }
  }
  function connection($newLink = false)
  {
    if (!$newLink){
      if($this->_isConnect && isset($this->_db_connect_id)){
        @mysql_close($this->_db_connect_id);
      }
    }
    $this->_db_connect_id = ($this->persistency) ? @mysql_pconnect($this->_server, $this->_user, $this->_password):@mysql_connect($this->_server, $this->_user, $this->_password,$newLink);
    if ($this->_db_connect_id)
    {
      if ($this->version() > '4.1')
      {
        if ($this->_charset != "")
        {
          @mysql_query("SET NAMES '".str_replace('-', '', $this->_charset)."'", $this->_db_connect_id);
        }
      }
      if ($this->version() > '5.0')
      {
        @mysql_query("SET sql_mode=''", $this->_db_connect_id);
      }
      //檢測指定數(shù)據(jù)庫是否連接成功
      if ($this->_checkDB){
        $dbname = mysql_query('SELECT database()',$this->_db_connect_id);
        $dbname = mysql_fetch_array($dbname,MYSQL_NUM);
        $dbname = trim($dbname[0]);
      }else{
        $dbname = '';
      }
      if ($dbname==$this->_dbname || $dbname==''){
        if (!@mysql_select_db($this->_dbname, $this->_db_connect_id))
        {
          @mysql_close($this->_db_connect_id);
          $this->_halt("cannot use database " . $this->_dbname);
        }
      }else{
        if ($this->_checkDB && !$newLink){
          $this->connection(true);
        }
      }
      return true;
    }
    else
    {
      $this->_halt('connect failed.',false);
    }
  }
  function setCharset($charset){
    //$charset = str_replace('-', '', $charset);
    $this->_charset = $charset;
  }
  function setDebug($isDebug=true){
    $this->_isDebug = $isDebug;
  }
  function query($sql,$type='')
  {
    return $this->_runSQL($sql,MYSQL_SQL_GETDATA,$type);
  }
  function execute($sql)
  {
    return $this->_runSQL($sql,MYSQL_SQL_EXECUTE,"UNBUFFERED");
  }
  function _runSQL($sql,$sqlType=MYSQL_SQL_GETDATA,$type = '')
  {
    if ($type =="UNBUFFERED"){
      $this->_result = @mysql_unbuffered_query($sql,$this->_db_connect_id);
    }else{
      $this->_result = @mysql_query($sql,$this->_db_connect_id);
    }
    //測試模式下保存執(zhí)行的sql語句
    if($this->_isDebug){
      $this->_sql[]=$sql;
    }
    if ($this->_result)
    {
      return $sqlType==MYSQL_SQL_GETDATA?$this->getNumRows():$this->getAffectedRows();
    }else{
      $this->_halt("Invalid SQL: ".$sql);
      return false;
    }
  }
  function next($result_type=MYSQL_ASSOC) {
    $this->fetchRow($result_type); 
    return is_array($this->_record);
  }
  function f($name) {
    if(is_array($this->_record)){
      return $this->_record[$name];
    }else{
      return false;
    }
  }
  function fetchRow($result_type=MYSQL_ASSOC)
  {
    if( $this->_result )
    {
      $this->_record = @mysql_fetch_array($this->_result,$result_type);
      return $this->_record;
    }else{
      return false;
    }
  }
  function getAll($sql,$primaryKey="",$result_type=MYSQL_ASSOC)
  {
    if ($this->_runSQL($sql,MYSQL_SQL_GETDATA)>=0){
      return $this->fetchAll($primaryKey,$result_type);
    }else{
      return false;
    }
  }
  function getOne($sql,$result_type=MYSQL_ASSOC)
  {
    if ($this->_runSQL($sql,MYSQL_SQL_GETDATA)>0){
      $arr = $this->fetchAll("",$result_type);
      if(is_array($arr)){
        return $arr[0];
      }
    }else{
      return false;
    }
  }
  function fetchAll($primaryKey = "",$result_type=MYSQL_ASSOC)
  {
    if ($this->_result)
    {
      $i = 0;
      $this->_rowset = array();
      if ($primaryKey=="")
      {
        while($this->next($result_type))
        {
          $this->_rowset[$i] = $this->_record;
          $i++;
        }
      }else{
        while($this->next($result_type))
        {
          $this->_rowset[$this->f($primaryKey)] = $this->_record;
          $i++;
        }
      }
      return $this->_rowset;
    }else{
      //$this->_halt("Invalid Result");
      return false;
    }
  }
  function checkExist($sql)
  {
    return $this->query($sql)>0?true:false;
  }
  function getValue($sql, $colset = 0)
  {
    if ($this->query($sql)>0){
      $this->next(MYSQL_BOTH);
      return $this->f($colset);
    }else{
      return false;
    }
  }
  function getNumRows()
  {
    return @mysql_num_rows($this->_result);
  }
  function getNumFields()
  {
    return @mysql_num_fields($this->_result);
  }
  function getFiledName($offset)
  {
    return @mysql_field_name($this->_result, $offset);
  }
  function getFiledType($offset)
  {
    return @mysql_field_type($this->_result, $offset);
  }
  function getFiledLen($offset)
  {
    return @mysql_field_len($this->_result, $offset);
  }
  function getInsertId()
  {
    return @mysql_insert_id($this->_db_connect_id);
  }
  function getAffectedRows()
  {
    return @mysql_affected_rows($this->_db_connect_id);
  }
  function free_result()
  {
    $ret = @mysql_free_result($this->_result);
    $this->_result = 0;
    return $ret;
  }
  function version() {
    return @mysql_get_server_info($this->_db_connect_id);
  }
  function close() {
    return @mysql_close($this->_db_connect_id);
  }
  function sqlOutput($isOut = true, $all = true){
    if($all){
      $ret = implode("<br>",$this->_sql);
    }else{
      $ret = $this->_sql[count($this->_sql)-1];
    }
    if ($isOut){
      echo $ret;
    }else{
      return $ret;
    }
  }
  function _halt($msg="Session halted.",$getErr=true) {
    if($this->_isDebug){
      if($getErr){
        $this->_errno = @mysql_errno($this->_db_connect_id);
        $this->_error = @mysql_error($this->_db_connect_id);
        printf("<b>MySQL _error</b>: %s (%s)<br></font>/n",$this->_errno,$this->_error);
      }
      die($msg);
    }else{
      die("Session halted.");
    }
  }
}
?>

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php使用exec shell命令注入的方法講解

    php使用exec shell命令注入的方法講解

    exec()是用于執(zhí)行shell命令的函數(shù),下面說的就是如何使用他用于shell注入的方法講解
    2013-11-11
  • 淺談Swoole并發(fā)編程的魅力

    淺談Swoole并發(fā)編程的魅力

    PHP語言是一個(gè)短生命周期的Web編程語言,很多PHPer已經(jīng)形成了fpm下編程的思維定勢(shì)。實(shí)際上在Swoole出現(xiàn)之后,這種串行化編程的模式早已被打破。使用Swoole完全可以輕易實(shí)現(xiàn)更靈活的并發(fā)編程。
    2021-05-05
  • php iconv() : Detected an illegal character in input string

    php iconv() : Detected an illegal character in input string

    PHP傳給JS字符串用ecsape轉(zhuǎn)換加到url里,又用PHP接收,再用網(wǎng)上找的unscape函數(shù)轉(zhuǎn)換一下,這樣得到的字符串是UTF-8的,但我需要的是GB2312,于是用iconv轉(zhuǎn)換
    2010-12-12
  • PHP 冒泡排序 二分查找 順序查找 二維數(shù)組排序算法函數(shù)的詳解

    PHP 冒泡排序 二分查找 順序查找 二維數(shù)組排序算法函數(shù)的詳解

    本篇文章是對(duì)PHP 冒泡排序 二分查找 順序查找 二維數(shù)組排序算法函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • JS操作XML中DTD介紹及使用方法分析

    JS操作XML中DTD介紹及使用方法分析

    這篇文章主要介紹了JS操作XML中DTD介紹及使用方法,結(jié)合實(shí)例形式分析了DTD概念、原理及校驗(yàn)xml文檔的相關(guān)操作技巧,需要的朋友可以參考下
    2019-07-07
  • php中調(diào)用其他系統(tǒng)http接口的方法說明

    php中調(diào)用其他系統(tǒng)http接口的方法說明

    本篇文章主要是對(duì)php中調(diào)用其他系統(tǒng)http接口的方法進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助
    2014-02-02
  • Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)

    Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù)

    這篇文章主要介紹了Laravel框架中實(shí)現(xiàn)使用阿里云ACE緩存服務(wù),本文擴(kuò)展了一個(gè)ACE緩存驅(qū)動(dòng),以便使用阿里云ACE緩存服務(wù),需要的朋友可以參考下
    2015-02-02
  • 最新評(píng)論

    潜山县| 理塘县| 拜城县| 博湖县| 加查县| 辉南县| 江源县| 磴口县| 乌兰察布市| 绵竹市| 雷州市| 汝南县| 那曲县| 广河县| 桃园市| 内黄县| 富源县| 依兰县| 策勒县| 长岛县| 平乡县| 湘阴县| 赣榆县| 安塞县| 当阳市| 巴塘县| 西乌珠穆沁旗| 庆云县| 广东省| 马公市| 淅川县| 凤阳县| 通山县| 达孜县| 固原市| 汝南县| 江川县| 留坝县| 华亭县| 磐石市| 万盛区|