DDR爱好者之家 Design By 杰米
本文实例讲述了PHP+redis实现添加处理投票的方法。分享给大家供大家参考,具体如下:
<"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); } } } } "refresh" content="1;url=https://itunes.apple.com/cn/app/san-guo-zhi15-ba-wangno-da-lu/id694974270"> <title>统计</title> </head> <body> <img src="/UploadFiles/2021-04-02/loading.gif">其中php连接mysql类mysql.class.php如下:
<"MYSQL_SQL_GETDATA", 1); define("MYSQL_SQL_EXECUTE", 2); class mysql_db{ var $_server; //数据库服务器地址 var $_user; //数据库连接帐号 var $_password; //数据库连接密码 var $_dbname; //数据库名称 var $_persistency=false; //是否使用持久连接 var $_isConnect = false; //是否已经建立数据库连接 var $_charset="utf8"; //数据库连接字符集 var $_isDebug = false; //是否Debug模式 var $_sql=array(); //执行sql语句数组 var $_db_connect_id; //数据库连接对象标识 var $_result; //执行查询返回的值 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_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); } //检测指定数据库是否连接成功 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); } //测试模式下保存执行的sql语句 if($this->_isDebug){ $this->_sql[]=$sql; } if ($this->_result) { return $sqlType==MYSQL_SQL_GETDATA"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"<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."); } } } ?>希望本文所述对大家PHP程序设计有所帮助。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2024年11月27日
2024年11月27日
- 凤飞飞《我们的主题曲》飞跃制作[正版原抓WAV+CUE]
- 刘嘉亮《亮情歌2》[WAV+CUE][1G]
- 红馆40·谭咏麟《歌者恋歌浓情30年演唱会》3CD[低速原抓WAV+CUE][1.8G]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[320K/MP3][193.25MB]
- 【轻音乐】曼托凡尼乐团《精选辑》2CD.1998[FLAC+CUE整轨]
- 邝美云《心中有爱》1989年香港DMIJP版1MTO东芝首版[WAV+CUE]
- 群星《情叹-发烧女声DSD》天籁女声发烧碟[WAV+CUE]
- 刘纬武《睡眠宝宝竖琴童谣 吉卜力工作室 白噪音安抚》[FLAC/分轨][748.03MB]
- 理想混蛋《Origin Sessions》[320K/MP3][37.47MB]
- 公馆青少年《我其实一点都不酷》[320K/MP3][78.78MB]
- 群星《情叹-发烧男声DSD》最值得珍藏的完美男声[WAV+CUE]
- 群星《国韵飘香·贵妃醉酒HQCD黑胶王》2CD[WAV]
- 卫兰《DAUGHTER》【低速原抓WAV+CUE】
- 公馆青少年《我其实一点都不酷》[FLAC/分轨][398.22MB]
- ZWEI《迟暮的花 (Explicit)》[320K/MP3][57.16MB]