简体   繁体   English

从 SQL 结果在数组中添加值

[英]adding values in an array from SQL result

I'm trying to put the user_value in an array if the affiliate_id=50.如果affiliate_id=50,我试图将user_value 放在一个数组中。 Then add up the array with the sum function.然后将数组与 sum 函数相加。 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?我的问题是如何将所有 user_values 加在一起(在一个数组中),其中affiliate_id=50?

Currently only one user_value is added to the array.目前只有一个 user_value 被添加到数组中。

https://imgur.com/a/r5lJRoA 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);

You can summarize values directly in MySQL:您可以直接在 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在这里尝试代码PHPize.online

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

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