[英]How to follow user id to get sum of MySQL column in PHP?
我在表中有一個列,我想按照“user id = 3”獲取 PHP 中 MySQL 列的總和。 但它不起作用。 下面是我的代碼:
<?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;"> *</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>
output 如下圖所示,沒有顯示總和值:
讓我們為您的列添加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.