更新时间:2022-05-02 22:46:20
您可以使用更新的存储过程来完成,然后将新值选择到输出参数中.以下返回一列 new_score
和新值.
You can do it with a stored procedure that updates, and then selects the new value into an output parameter.
The following returns one column new_score
with the new value.
DELIMITER $$ -- Change DELIMITER in order to use ; withn the procedure
CREATE PROCEDURE increment_score
(
IN id_in INT
)
BEGIN
UPDATE item SET score = score + 1 WHERE id = id_in;
SELECT score AS new_score FROM item WHERE id = id_in;
END
$$ -- Finish CREATE PROCEDURE statement
DELIMITER ; -- Reset DELIMITER to standard ;
在 PHP 中:
$result = mysql_query("CALL increment_score($id)");
$row = mysql_fetch_array($result);
echo $row['new_score'];