Alexandro Setiawan
Alexandro Setiawan

Reputation: 96

How to multiply all row MySQL and PHP

i want to multiply all row MySQL with PHP

I have database named count and the example MySQL table named tblcount :

|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

Upvotes: 1

Views: 493

Answers (2)

Sougata Bose
Sougata Bose

Reputation: 31749

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)");

Upvotes: 0

kamal pal
kamal pal

Reputation: 4207

You can do it in sql query, no need of php intraction. Try below Query

UPDATE tblcount SET result=number1*number2

Upvotes: 2

Related Questions