Skip to content
Advertisement

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 :

enter image description here

Below is my database information, actually I want to select user_id = 3 and to total up “total_amount”: enter image description here

Advertisement

Answer

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'];    
    }
?>
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement