首页 > 开发 > PHP > 正文

PHP+redis实现添加处理投票的方法

2024-05-04 23:40:24
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了PHP+redis实现添加处理投票的方法,结合实例较为详细的分析了PHP+redis数据库操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了PHP+redis实现添加处理投票的方法。分享给大家供大家参考,具体如下:

 

 
  1. <?php 
  2. header("Content-Type:text/html;charset=utf-8"); 
  3. include 'lib/mysql.class.php'; 
  4. $mysql_obj = mysql::getConn(); 
  5. if(class_exists('Redis')){ 
  6. //redis  
  7. $redis = new Redis(); 
  8. $redis->pconnect('127.0.0.1', 6379); 
  9. if(isset($_SERVER['HTTP_REFERER'])){ 
  10. $url_md5 = md5($_SERVER['HTTP_REFERER']); 
  11. } 
  12. $adve_key = 'adve';  
  13. $adve_key_exists = 'adve_exists'; 
  14. if(!$redis->exists($adve_key_exists)){ 
  15. $list = $mysql_obj->fetch_array("select * from admin_online_adve"); 
  16. if($list){ 
  17. foreach ($list as $key => $value) { 
  18. $url_hash = md5($value['adve_url']); 
  19. $adve_hash_key = $adve_key.":".$url_hash; 
  20. $id = $value['id']; 
  21. $redis->set($adve_hash_key,$id); 
  22. $redis->set($adve_key_exists,true); 
  23. } 
  24. } 
  25. } 
  26. $adve_new_key = $adve_key.':'.$url_md5; 
  27. if($redis->exists($adve_new_key)){ 
  28. $adve_plus = $adve_new_key.":plus" ; 
  29. if(!$redis->exists($adve_plus)){ 
  30. $redis->set($adve_plus,1);  
  31. }else{ 
  32. $redis->incr($adve_plus); 
  33. $num = $redis->get($adve_plus); 
  34. if($num >100){ 
  35. $id = $redis->get($adve_new_key); 
  36. // insert to sql; 
  37. $mysql_obj->query("update admin_online_adve set adve_num=adve_num+$num where id=$id"); 
  38. $redis->set($adve_plus,1); 
  39. } 
  40. } 
  41. } 
  42. } 
  43. ?> 
  44. <html> 
  45. <head> 
  46. <meta http-equiv="refresh" content="1;url=https://itunes.apple.com/cn/app/san-guo-zhi15-ba-wangno-da-lu/id694974270?mt=8"> 
  47. <title>统计</title> 
  48. </head> 
  49. <body> 
  50. <img src="loading.gif">Loading... 
  51. </body> 
  52. </html> 

其中php连接mysql类mysql.class.php如下:

 

 
  1. <?php 
  2. define("MYSQL_SQL_GETDATA", 1); 
  3. define("MYSQL_SQL_EXECUTE", 2); 
  4. class mysql_db{ 
  5. var $_server; //数据库服务器地址 
  6. var $_user; //数据库连接帐号 
  7. var $_password; //数据库连接密码 
  8. var $_dbname; //数据库名称 
  9. var $_persistency=false; //是否使用持久连接 
  10. var $_isConnect = false; //是否已经建立数据库连接 
  11. var $_charset="utf8"; //数据库连接字符集 
  12. var $_isDebug = false; //是否Debug模式 
  13. var $_sql=array(); //执行sql语句数组 
  14. var $_db_connect_id; //数据库连接对象标识 
  15. var $_result; //执行查询返回的值 
  16. var $_record; 
  17. var $_rowset; 
  18. var $_errno = 0; 
  19. var $_error = "connection error"; 
  20. var $_checkDB = false; 
  21. function mysql_db($dbserver, $dbuser, $dbpassword,$database,$persistency = false,$autoConnect=false,$checkdb = false) 
  22. { 
  23. $this->_server = $dbserver; 
  24. $this->_user = $dbuser; 
  25. $this->_password = $dbpassword; 
  26. $this->_dbname = $database; 
  27. $this->_persistency = $persistency; 
  28. $this->_autoConnect = $autoConnect; 
  29. $this->_checkDB = $checkdb; 
  30. if($autoConnect){ 
  31. $this->connection(); 
  32. } 
  33. } 
  34. function connection($newLink = false) 
  35. { 
  36. if (!$newLink){ 
  37. if($this->_isConnect && isset($this->_db_connect_id)){ 
  38. @mysql_close($this->_db_connect_id); 
  39. } 
  40. } 
  41. $this->_db_connect_id = ($this->persistency) ? @mysql_pconnect($this->_server, $this->_user, $this->_password):@mysql_connect($this->_server, $this->_user, $this->_password,$newLink); 
  42. if ($this->_db_connect_id) 
  43. { 
  44. if ($this->version() > '4.1') 
  45. { 
  46. if ($this->_charset != "") 
  47. { 
  48. @mysql_query("SET NAMES '".str_replace('-', '', $this->_charset)."'", $this->_db_connect_id); 
  49. } 
  50. } 
  51. if ($this->version() > '5.0') 
  52. { 
  53. @mysql_query("SET sql_mode=''", $this->_db_connect_id); 
  54. } 
  55. //检测指定数据库是否连接成功 
  56. if ($this->_checkDB){ 
  57. $dbname = mysql_query('SELECT database()',$this->_db_connect_id); 
  58. $dbname = mysql_fetch_array($dbname,MYSQL_NUM); 
  59. $dbname = trim($dbname[0]); 
  60. }else{ 
  61. $dbname = ''; 
  62. } 
  63. if ($dbname==$this->_dbname || $dbname==''){ 
  64. if (!@mysql_select_db($this->_dbname, $this->_db_connect_id)) 
  65. { 
  66. @mysql_close($this->_db_connect_id); 
  67. $this->_halt("cannot use database " . $this->_dbname); 
  68. } 
  69. }else{ 
  70. if ($this->_checkDB && !$newLink){ 
  71. $this->connection(true); 
  72. } 
  73. } 
  74. return true; 
  75. } 
  76. else 
  77. { 
  78. $this->_halt('connect failed.',false); 
  79. } 
  80. } 
  81. function setCharset($charset){ 
  82. //$charset = str_replace('-', '', $charset); 
  83. $this->_charset = $charset; 
  84. } 
  85. function setDebug($isDebug=true){ 
  86. $this->_isDebug = $isDebug; 
  87. } 
  88. function query($sql,$type='') 
  89. { 
  90. return $this->_runSQL($sql,MYSQL_SQL_GETDATA,$type); 
  91. } 
  92. function execute($sql) 
  93. { 
  94. return $this->_runSQL($sql,MYSQL_SQL_EXECUTE,"UNBUFFERED"); 
  95. } 
  96. function _runSQL($sql,$sqlType=MYSQL_SQL_GETDATA,$type = '') 
  97. { 
  98. if ($type =="UNBUFFERED"){ 
  99. $this->_result = @mysql_unbuffered_query($sql,$this->_db_connect_id); 
  100. }else{ 
  101. $this->_result = @mysql_query($sql,$this->_db_connect_id); 
  102. } 
  103. //测试模式下保存执行的sql语句 
  104. if($this->_isDebug){ 
  105. $this->_sql[]=$sql; 
  106. } 
  107. if ($this->_result) 
  108. { 
  109. return $sqlType==MYSQL_SQL_GETDATA?$this->getNumRows():$this->getAffectedRows(); 
  110. }else{ 
  111. $this->_halt("Invalid SQL: ".$sql); 
  112. return false; 
  113. } 
  114. } 
  115. function next($result_type=MYSQL_ASSOC) { 
  116. $this->fetchRow($result_type);  
  117. return is_array($this->_record); 
  118. } 
  119. function f($name) { 
  120. if(is_array($this->_record)){ 
  121. return $this->_record[$name]; 
  122. }else{ 
  123. return false; 
  124. } 
  125. } 
  126. function fetchRow($result_type=MYSQL_ASSOC) 
  127. { 
  128. if( $this->_result ) 
  129. { 
  130. $this->_record = @mysql_fetch_array($this->_result,$result_type); 
  131. return $this->_record; 
  132. }else{ 
  133. return false; 
  134. } 
  135. } 
  136. function getAll($sql,$primaryKey="",$result_type=MYSQL_ASSOC) 
  137. { 
  138. if ($this->_runSQL($sql,MYSQL_SQL_GETDATA)>=0){ 
  139. return $this->fetchAll($primaryKey,$result_type); 
  140. }else{ 
  141. return false; 
  142. } 
  143. } 
  144. function getOne($sql,$result_type=MYSQL_ASSOC) 
  145. { 
  146. if ($this->_runSQL($sql,MYSQL_SQL_GETDATA)>0){ 
  147. $arr = $this->fetchAll("",$result_type); 
  148. if(is_array($arr)){ 
  149. return $arr[0]; 
  150. } 
  151. }else{ 
  152. return false; 
  153. } 
  154. } 
  155. function fetchAll($primaryKey = "",$result_type=MYSQL_ASSOC) 
  156. { 
  157. if ($this->_result) 
  158. { 
  159. $i = 0; 
  160. $this->_rowset = array(); 
  161. if ($primaryKey=="") 
  162. { 
  163. while($this->next($result_type)) 
  164. { 
  165. $this->_rowset[$i] = $this->_record; 
  166. $i++; 
  167. } 
  168. }else{ 
  169. while($this->next($result_type)) 
  170. { 
  171. $this->_rowset[$this->f($primaryKey)] = $this->_record; 
  172. $i++; 
  173. } 
  174. } 
  175. return $this->_rowset; 
  176. }else{ 
  177. //$this->_halt("Invalid Result"); 
  178. return false; 
  179. } 
  180. } 
  181. function checkExist($sql) 
  182. { 
  183. return $this->query($sql)>0?true:false; 
  184. } 
  185. function getValue($sql, $colset = 0) 
  186. { 
  187. if ($this->query($sql)>0){ 
  188. $this->next(MYSQL_BOTH); 
  189. return $this->f($colset); 
  190. }else{ 
  191. return false; 
  192. } 
  193. } 
  194. function getNumRows() 
  195. { 
  196. return @mysql_num_rows($this->_result); 
  197. } 
  198. function getNumFields() 
  199. { 
  200. return @mysql_num_fields($this->_result); 
  201. } 
  202. function getFiledName($offset) 
  203. { 
  204. return @mysql_field_name($this->_result, $offset); 
  205. } 
  206. function getFiledType($offset) 
  207. { 
  208. return @mysql_field_type($this->_result, $offset); 
  209. } 
  210. function getFiledLen($offset) 
  211. { 
  212. return @mysql_field_len($this->_result, $offset); 
  213. } 
  214. function getInsertId() 
  215. { 
  216. return @mysql_insert_id($this->_db_connect_id); 
  217. } 
  218. function getAffectedRows() 
  219. { 
  220. return @mysql_affected_rows($this->_db_connect_id); 
  221. } 
  222. function free_result() 
  223. { 
  224. $ret = @mysql_free_result($this->_result); 
  225. $this->_result = 0; 
  226. return $ret; 
  227. } 
  228. function version() { 
  229. return @mysql_get_server_info($this->_db_connect_id); 
  230. } 
  231. function close() { 
  232. return @mysql_close($this->_db_connect_id); 
  233. } 
  234. function sqlOutput($isOut = true, $all = true){ 
  235. if($all){ 
  236. $ret = implode("<br>",$this->_sql); 
  237. }else{ 
  238. $ret = $this->_sql[count($this->_sql)-1]; 
  239. } 
  240. if ($isOut){ 
  241. echo $ret; 
  242. }else{ 
  243. return $ret; 
  244. } 
  245. } 
  246. function _halt($msg="Session halted.",$getErr=true) { 
  247. if($this->_isDebug){ 
  248. if($getErr){ 
  249. $this->_errno = @mysql_errno($this->_db_connect_id); 
  250. $this->_error = @mysql_error($this->_db_connect_id); 
  251. printf("<b>MySQL _error</b>: %s (%s)<br></font>/n",$this->_errno,$this->_error); 
  252. } 
  253. die($msg); 
  254. }else{ 
  255. die("Session halted."); 
  256. } 
  257. } 
  258. } 
  259. ?> 

希望本文所述对大家PHP程序设计有所帮助。


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表