从MySQL表中获取价值,将其相乘并再次存储

时间:2013-12-13 14:02:13

标签: php mysql

我是新手,需要有关PHP MySQL的帮助:

我在MySQL中有一个像这样的表:

Number1 | Number2 |计算|
   8 | 9 | |
   5 | 6 | |
   3 | 2 | |

使用PHP,如何获取Number1和Number2值,将每行相乘并将其存储到Calculate列?

这是我的代码:

$con = mysqli_connect("localhost","root","","db");
$query = mysql_query("SELECT * FROM Multiply");
while($result = mysql_fetch_assoc($query)); {
    $Num1=$result['Number1']; $Num2=$result['Number2'];
    $result=$Num1+Num2;
    mysqli_query($con,"UPDATE Multiply SET calculate=$result");
}

感谢您的帮助。

3 个答案:

答案 0 :(得分:3)

$con = mysqli_connect("localhost","root","","db");
$query = mysqli_query($con,"UPDATE Multiply SET Calculate = Number1 * Number2");

答案 1 :(得分:1)

你甚至不需要通过php,或者你可以从php调用查询

update Multiply set calculate=number1*number2

答案 2 :(得分:1)

更改

$result=$Num1*Num2;

$result=$Num1*$Num2;

或者只运行以下的sql

"UPDATE Multiply SET Calculate = Number1 * Number2"