MYSQL列值增加100时执行某些操作 [英] Do Something When MYSQL Column Value Increments by 100

查看:75
本文介绍了MYSQL列值增加100时执行某些操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在站点成员的帖子数达到100的倍数时提醒站点的成员.当mysql表中的值达到100、200、300等时,是否可以运行函数或回显某些内容?

表结构:

 用户名|密码电邮|帖子 


我目前正在使用以下信息显示他们的帖子数:

  $ sql = mysql_query("SELECT`posts from`user` WHERE username ='$ username'")或die(mysql_error());$ row = mysql_fetch_array($ sql);回声< br/>< h4>您的帖子:</h4>".".$ row ['posts']; 



已解决:我接受了Marc提出的使用模数的建议,该模数适合我的需求.

  $ sql = mysql_query("SELECT`posts` AS post from`user` WHERE username ='$ username'")或die(mysql_error());$ row = mysql_fetch_array($ sql);$ post = $ row ['posts'];if($ post%100 == 0){mysql_query("INSERT INTO`posts(name,message,message_raw)VALUES('System','$ name has been $ post posts!','$ name has been $ post posts!')");} 

解决方案

您需要



RESOLVED: I went with Marc's suggestion of using modulo, which suited my needs.

$sql = mysql_query("SELECT `posts` AS posts FROM `user` WHERE username='$username'") or die(mysql_error());
$row = mysql_fetch_array($sql);
$post = $row['posts'];

    if($post % 100 == 0) {

        mysql_query("INSERT INTO `posts` (name, message, message_raw) VALUES ('System', '$name has reached $post posts!', '$name has reached $post posts!')");

    }

You need a modulo

Modulo operation. Returns the remainder of N divided by M.

Like this

SELECT username
FROM `user` 
WHERE username='$username' AND MOD(posts,100) = 0

And you can check the condition every time a user do a new post

这篇关于MYSQL列值增加100时执行某些操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆