增加登录次数

时间:2012-01-05 18:38:35

标签: php codeigniter

我不太确定为什么我的查询不会将登录次数增加一次甚至不将它附加到查询字符串上。

public function update_logins($user_id)
{
    $this->db->set('number_of_logins', 'number_of_logins'+1);
    $this->db->where('user_id', $user_id);   
    $this->db->update('users_logins');
    echo $this->db->last_query();
}

2 个答案:

答案 0 :(得分:1)

虽然我不知道codeigniter如何处理评论中的建议,我可以告诉你的是:

您对'number_of_logins'的+1不会产生预期的行为,因为字符串+数字通常会产生意外结果,具体取决于字符串的内容。

PHP中的数字解析通过扫描字符串中的数字和数字符号来实现。它找到的任何内容都将被视为数字的可能部分,直到找到无效字符为止。

在带有“number_of_logins”的字符串的上下文中,它将产生值0,因为该字符串中没有任何内容允许数字解释。但是,“10_number_of_logins”将生成10,因此添加1将使11。

<强>更新

例如:

(int)"3 pigs" = 3 //The first character is valid "3" but the space is not
(int)"Three pigs" = 0 //The first character "T" is invalid, thus number is 0
(int)"17.1 percent" = 17.1 //Period is a valid character
(int)"17,1 percent" = 17 //Comma is not a valid character
(int)"I fly with 3 pigs" = 0; //Even if 3 is in the string, it starts with "I" which is invalid

希望这有帮助

答案 1 :(得分:1)

这可能是由number_of_logins设置为null(null + 1 = null)引起的。如果是这样,请将number_of_logins的默认值设置为0,而不将该字段设为空;或者(如果你使用的是mysql)试试:

  

$ this-&gt; db-&gt; set('number_of_logins','coalesce(number_of_logins,0)+1');

以下是coalesce的更多信息。