Php+redis implementation Add process voting method _php Tips

Source: Internet
Author: User
Tags md5 php define php programming redis

The example in this article describes how the Php+redis implementation adds processing votes. Share to everyone for your reference, specific as follows:

<?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. ":p LUs"; 
    if (! $redis->exists ($adve _plus)) {$redis->set ($adve _plus,1); }else{$redis-&GT;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); }}}?>  

Where PHP connection MySQL class mysql.class.php is as follows:

<?php define ("Mysql_sql_getdata", 1);
Define ("Mysql_sql_execute", 2);        Class mysql_db{var $_server;         Database server address var $_user;       Database connection account Var $_password;        Database connection password Var $_dbname;  Database name Var $_persistency=false;  Whether to use persistent connection var $_isconnect = false;    Whether the database has been established to connect var $_charset= "UTF8";   Database connection Character Set var $_isdebug = false;     Whether the debug mode var $_sql=array ();    Executes an array of SQL statements Var $_db_connect_id;        The database Connection object identifies Var $_result;
  Executes the value returned by the query 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); //Detect if the specified database is connected successfully if ($this->_checkdb) {$dbname = mysql_query (' SELECT db () ', $this->_db_con
        NECT_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_con
          nect_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->_res
    Ult = @mysql_unbuffered_query ($sql, $this->_db_connect_id);
    }else{$this->_result = @mysql_query ($sql, $this->_db_connect_id);
    ///test mode save executed SQL statement if ($this->_isdebug) {$this->_sql[]= $sql; } if ($this->_result) {return $sqlType ==mysql_sql_getdata $this->getnumrows (): $this->getaffectedro
    WS ();
      }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; The 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;
      The 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;
      The function GetValue ($sql, $colset = 0) {if ($this->query ($sql) >0) {$this->next (mysql_both);
    return $this->f ($colset);
    }else{return false;
  The 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);
    The 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; The 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.");

 }}}?>

I hope this article will help you with your PHP programming.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.