Getting SUM of Price using Array


I am trying to get the sum of an array of numbers I have collected. I have used this array in in another query, so I know that works correctly. But I may be using it wrong in this instance for a sum? Appreciate any help.

$result = mysqli_query("SELECT SUM(price) AS price_sum FROM products where prodID IN (".implode(',',$arrayID).")"); 
$row = mysqli_fetch_assoc($result); 
$sum = $row['value_sum'];
	echo  $sum;

Why are you trying to get the value of, value_sum, but using the alias, price_sum?


Yeah, Supposed to be price_sum

I see the problem, It is the way the array is displayed.
So the numbers that are coming from the array are 1 and 9

Currently the array is imploding as 1,9
I need it to implode as ‘1’, ‘9’

How would I do that? Many thanks.


It is 1 and 9, both integers. Why do you need them as ‘1’ and ‘9’?


No. Only strings needs to be enclosed in single-quotes. Numbers do not.

However, if the values in $arrayID are coming from external/unknown data, you should not put them directly into the sql query statement as this will allow sql injection (your current query would allow a UNION query to be injected which can grab all the data from any of your database tables and display it.) You should be using a prepared query when supplying external/unknown data to an sql query statement.