PHP/MySQL 中的计数器不递增

发布于 2024-12-10 13:03:48 字数 1095 浏览 1 评论 0原文

我需要为会员部分创建一个计数器(计算用户登录的次数)。

我有以下脚本(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
?>

DB:

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]

但由于某种原因,我的计数器无法正确计数。我缺少什么?

I need to create a counter for member section (count the number of times a user logged).

I have the following script (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
?>

DB:

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

Now, what I do in my .htaccess file is:

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

but for some reason my counter does not count correctly. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

携君以终年 2024-12-17 13:03:48

您可能可以简化它并执行以下操作:

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

if (mysql_affected_rows() == 0) {
   mail(ADMIN, 'Cannot Get: ' . mysql_error(), mysql_error());
}

mysql_close($conn);

无需进行初始检查。

You could probably just simplify it and do the following:

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

if (mysql_affected_rows() == 0) {
   mail(ADMIN, 'Cannot Get: ' . mysql_error(), mysql_error());
}

mysql_close($conn);

No need to do initial check.

入怼 2024-12-17 13:03:48

使用此

$count = $row['views'] + 1;

$count = ++$row['views'];

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

语法:

$x = 1;
$count = $x++;
// $count = 1

$x = 1;
$count = ++$x;
// $count = 2

use this

$count = $row['views'] + 1;

or

$count = ++$row['views'];

or

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

syntax:

$x = 1;
$count = $x++;
// $count = 1

$x = 1;
$count = ++$x;
// $count = 2
月寒剑心 2024-12-17 13:03:48

问题出在

$count = $row['views']++; 

这行实际上说的是:
- 将view的值赋给$count
- 增加观看次数。

但你想要:

$count = ++$row['views']; 

上面写着:
- 增加观看次数。
- 将 view 的(递增的)值分配给 $count

一个细微的差别。 :~)

The problem is in the line

$count = $row['views']++; 

This actually says:
- Assign the value of view to $count
- Increment views.

But you want:

$count = ++$row['views']; 

Which says:
- Increment views.
- Assign the (incremented) value of view to $count

A subtle difference. :~)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文