简体   繁体   中英

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. 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:

在此处输入图像描述

Below is my database information, actually I want to select user_id = 3 and to total up "total_amount": 在此处输入图像描述

Lets add alias to your column and get the column value based on 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'];    
    }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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