计数器在PHP / MySQL中不会增加

前端之家收集整理的这篇文章主要介绍了计数器在PHP / MySQL中不会增加前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我需要为成员部分创建一个计数器(计算用户记录的次数).

我有以下脚本(counter.PHP):

PHP
    $conn = MysqL_connect("localhost","myuser","mypass");
    MysqL_select_db("test");

    $sql = "SELECT views FROM members WHERE mid = " . $_GET['mid'];     
    $result = MysqL_query($sql); 
    if (!$result)
        {
        mail(ADMIN,'Cannot Get: ' . MysqL_error(),MysqL_error());  
        }
    while ($row = MysqL_fetch_assoc($result)) 
        {
        $count = $row['views']++; 
        }
    $query = "UPDATE members SET views = '$count' WHERE mid = " . $_GET['mid']; 
    MysqL_query($query); 
    MysqL_close($conn);

    // show the logo using header() and readfile(); // that part work
?>

D B:

CREATE TABLE `members` (
  `mid` int(11) NOT NULL AUTO_INCREMENT,`views` int(11) DEFAULT '0',/* etc...*/
  PRIMARY KEY (`mid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

现在,我在.htaccess文件中所做的是:

RewriteEngine On
RewriteRule ^img/logo([0-9]+).jpg$/counter.PHP?mid=$1 [L]

但由于某种原因,我的计数器不正确.我错过了什么?

最佳答案
你可能只是简化它并执行以下操作:

$query = "UPDATE members SET views = views + 1 WHERE mid = " . $_GET['mid']; 
MysqL_query($query); 

if (MysqL_affected_rows() == 0) {
   mail(ADMIN,MysqL_error());
}

MysqL_close($conn);

无需进行初步检查.

原文链接:https://www.f2er.com/mysql/434267.html

猜你在找的MySQL相关文章