简体   繁体   English

如何将所有行与MySQL和PHP相乘

[英]How to multiply all row MySQL and PHP

i want to multiply all row MySQL with PHP 我想用PHP将所有行MySQL相乘

I have database named count and the example MySQL table named tblcount : 我有一个名为count的数据库和一个名为tblcount的示例MySQL表:

|id|number1|number2|result|
|1 |   4   |   1   |      |
|2 |   5   |   4   |      |
|3 |   6   |   3   |      |

i want to be : 我想成为 :

|id|number1|number2|result|
|1 |   4   |   1   |  4   |
|2 |   5   |   4   |  20  |
|3 |   6   |   3   |  18  |

i already used this code, but the code is not run correctly : 我已经使用过此代码,但是代码无法正确运行:

<?php
require 'database/db.php';

$count = $mysqli->query("SELECT * FROM tblcount");
$row = mysqli_fetch_assoc($count);

$result = $row['number1'] * $row['number2'];

$mysqli->query("UPDATE tblcount
                SET result = '".$result."'");
?>

what's the wrong? 怎么了 Thank you 谢谢

You can do it in sql query, no need of php intraction. 您可以在sql查询中完成此操作,而无需php吸引力。 Try below Query 尝试以下查询

UPDATE tblcount SET result=number1*number2

You can multiply them with in the query. 您可以在查询中将它们与相乘。 Not only multiple, the other operations also. 不仅多个,其他操作也。 Try with this - 试试这个-

$mysqli->query("UPDATE tblcount SET result = (number1 * number2)");

If you want to multiply with other table columns then - 如果要与其他表列相乘,则-

$mysqli->query("UPDATE tblcount tc, tblother to SET tc.result = (to.field1 * to.field2)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM