PHP + redis add methods for voting

Source: Internet
Author: User
This article mainly introduces how to add and handle voting in PHP + redis, and analyzes the related skills of PHP + redis database operations in detail based on the instance, which has some reference value, for more information, see

This article mainly introduces how to add and handle voting in PHP + redis, and analyzes the related skills of PHP + redis database operations in detail based on the instance, which has some reference value, for more information, see

<? 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 ('2017. 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) ;}}}?> StatisticsLoading...

The php connection to mysql class mysql. class. php is as follows:

<? Phpdefine ("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 a database connection has been established var $ _ charset = "utf8 "; // database connection Character Set var $ _ isDebug = false; // whether Debug mode var $ _ SQL = array (); // execute the SQL statement array var $ _ db_connect_id; // ID of the database connection object var $ _ result; // The value returned by the execution 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 );} // check whether the specified database is successfully connected 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, M YSQL_ 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);} // Save the executed SQL statement if ($ this-> _ isDebug) in Test Mode) {$ 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_fe Tch_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 ("
", $ 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 ("MySQL _ error: % S (% s)
/N ", $ this-> _ errno, $ this-> _ error);} die ($ msg);} else {die (" Session halted. ") ;}}?>

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.