Skip to content
Advertisement

adding values in an array from SQL result

I’m trying to put the user_value in an array if the affiliate_id=50. Then add up the array with the sum function. The issue is the array only has one of the user values inside it.

My question is how do I add all the user_values together (in an array) WHERE the affiliate_id=50?

Currently only one user_value is added to the array.

https://imgur.com/a/r5lJRoA

$sql = mysqli_query($conn, "SELECT user_value FROM active_users WHERE affiliate_id=50"); 
$affiliate_total_earnt = array(mysqli_fetch_assoc($sql));



echo array_sum($affiliate_total_earnt);

Advertisement

Answer

You can summarize values directly in MySQL:

$query = "SELECT sum(user_value) as sum FROM active_users WHERE affiliate_id=50";
$result = mysqli_query($conn, $query); 
$affiliate_total_earn = mysqli_fetch_assoc($result)['sum'];
    
echo "Affiliate total earn: " . $affiliate_total_earn;

Try code here PHPize.online

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement