0

I'm sorry if this question is duplicate or has been asked before. I've read some references on the internet, but still don't get it.

So, I have a transaction table:

id customer qty date
1 cus1 5 2021-04-01
2 cus2 3 2021-04-01
3 cus3 7 2021-04-02

I want to take all the qty data where date = "2021-04-01", then I want to sum it. So, I'm using the following code:

$qty = $db->query("SELECT SUM(`qty`) FROM `transaction` WHERE `date` = '2021-04-01'");

Note: the $db variable is the connection function to the database

Next, I want to multiply the results of the query, as follows:

$profit = $qty * 10;

And when I run the code I get an error message:

Notice: Object of class mysqli_result could not be converted to number

How do I fix it? All I want is to multiply the query result from the variable $qty ($qty * 10)

I am a beginner. Any help will be very much appreciated. Thanks, guys.

1 Answers1

0

I updated your SQL query to alias the aggregation for easy access.

$qty = $db->query("SELECT SUM(`qty`) as 'val_sum' FROM `transaction` WHERE `date` = '2021-04-01'");

You can cast a string to an int with "(int) $string_value"

if ($qty->num_rows > 0) {
   $row = $qty->fetch_assoc()
   $profit = (int) $row['val_sum'] * 10;
}