问题
I need to alert members of a site when their post count has reached a multiple of 100. Is it possible to run a function or echo something when a value in a mysql table reaches 100, 200, 300, etc?
Table structure:
username | password | email | posts
I'm currently displaying their post count with:
$sql = mysql_query("SELECT `posts` FROM `user` WHERE username='$username'") or die(mysql_error());
$row = mysql_fetch_array($sql);
echo "<br /><h4>Your posts:</h4>" . " " . $row['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!')");
}
回答1:
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
回答2:
You can use triggers:
http://dev.mysql.com/doc/refman/5.5/en/triggers.html
or
Just create a cron job
来源:https://stackoverflow.com/questions/13590389/do-something-when-mysql-column-value-increments-by-100