subtract “1” from the number in a row SQL Query

2020-04-18 08:43发布

I just want to know how to subtract 1 from the number that appears in $row[posts_remaining]

In other words...


<?php
$con = mysql_connect($db_server_name,$db_username,$db_password);
if (!$con) {
    //do something
}
mysql_select_db("" . $db_database_name . "", $con);
$result = mysql_query("SELECT * FROM users
WHERE fb_id='$user_id'");
while($row = mysql_fetch_array($result)) {
    $posts_remaining = $row['posts_remaining']
    // this is where I want to subtract 1 and then update "posts_remaining" with the new number
}

mysql_close($con);
?>

This will give me my result where row posts_remaining = {THE NUMBER}

But I want to update that returned number by subtracting one from it and then SETting the new number in back where the old number was.

I hope Im not making this confusing. It's hard to explain.

Additionally... should I have the row "posts_remaining" set as something other than TEXT in order to do this.... like Char(50) or something or is it ok to leave it as TEXT?

标签: php mysql sql
2条回答
我欲成王,谁敢阻挡
2楼-- · 2020-04-18 09:22

First you have to modify the type of the field from text to integer Than for update records try this query

update users
set
posts_remaining= posts_remaining - 1
WHERE fb_id='your_user_id'
查看更多
对你真心纯属浪费
3楼-- · 2020-04-18 09:32

You can do it using database query itself:

mysql_query("Update users set posts_remaining = posts_remaining -1 where fb_id='$user_id'");
查看更多
登录 后发表回答