Ways to Php+redis Database operations

Source: Internet
Author: User
This article mainly introduces the method of Php+redis database operation, the interested friend's reference, hope to be helpful to everybody.

The example in this article describes the method by which the Php+redis implementation adds processing votes. Share to everyone for your reference, 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->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 connects MySQL class mysql.class.php as follows:

<?phpdefine ("Mysql_sql_getdata", 1);d efine ("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 connection has been established 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      ); }//detects if the specified database is successfully connected if ($this->_checkdb) {$dbname = mysql_query (' SELECT database () ', $this->_db_connec        T_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_conne          ct_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);    }//Test mode saves the executed SQL statement 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-&gt          ; _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."); }}}?>

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.