简体   繁体   English

如何按照用户 ID 获取 PHP 中 MySQL 列的总和?

[英]How to follow user id to get sum of MySQL column in PHP?

I have a column in a table that I want to follow "user id = 3" to get sum of MySQL column in PHP.我在表中有一个列,我想按照“user id = 3”获取 PHP 中 MySQL 列的总和。 but it's not working.但它不起作用。 Below is my code:下面是我的代码:

     <?php
    $sql_select_point = 'SELECT SUM(total_amount) FROM bp_roi WHERE user_id = ' . $user_id; // when I login, $user_id will store my user id number. Now my login user_id is 3.

    $query_select_point = db_conn_select($sql_select_point);
    foreach($query_select_point as $rs_select_point) {
    $bonus_point = $rs_select_point; //I want to store and get sum of MySQL column in $bonus_point
    }

    ?>


      <div class="form-group">
                        <label for="recipient-name" class="col-form-label">Bonus Point Balance:<span style="color:red;">&nbsp;*</span></label>
                        <div class="col-form-label">
                            <input class="form-group form-control" name="bonus_point" id="bonus_point" value="<?php echo $bonus_point; ?>" readonly></input> //I can't echo $bonus_point in the input.
                        </div>
                    </div>

The output show me like below the picuture and no show total sum up value: output 如下图所示,没有显示总和值:

在此处输入图像描述

Below is my database information, actually I want to select user_id = 3 and to total up "total_amount":下面是我的数据库信息,实际上我想要 select user_id = 3 并总计“total_amount”: 在此处输入图像描述

Lets add alias to your column and get the column value based on index .让我们为您的列添加alias并根据index获取列值。

<?php
    $sql_select_point = 'SELECT SUM(total_amount) as tAmount FROM bp_roi WHERE user_id = ' . $user_id; 

    $query_select_point = db_conn_select($sql_select_point);
    foreach($query_select_point as $rs_select_point) {
        $bonus_point = $rs_select_point['tAmount'];    
    }
?>

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

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