我的SUM(总计)显示所有用户的总和

时间:2017-08-04 15:15:17

标签: php row

我让它努力显示总和。但它包括所有用户。我希望它仅通过登录用户显示总和。请帮帮我!

<?php  
  $conn = new mysqli($servername, $username, $password, $dbname); 
  foreach($conn->query('SELECT total FROM event') as $row) {  
  echo "<td><span style=font-weight:bold> " . number_format($row['total'],2) . "</span></td>";  
  }  
?> 

3 个答案:

答案 0 :(得分:0)

试试这段代码:

<?php  
  $user_id = 5; //set login user id here
  $conn = new mysqli($servername, $username, $password, $dbname); 
  foreach($conn->query('SELECT COUNT(event_id) as total FROM event WHERE user_id ='.$user_id) as $row) {  
    echo "<td><span style=font-weight:bold>" . number_format($row['total'], 2) . "</span></td>";  
  }  
?> 

答案 1 :(得分:0)

试试此代码

使用此查询获取当前用户总数

"Select sum(total) from event where name='$_SESSION['username']'";

使用此网址:http://www.sqlcourse2.com/agg_functions.html

答案 2 :(得分:0)

试用此代码

<?php  $user_id = 1;
$conn = new mysqli($servername, $username, $password, $dbname);
$query = $conn->query("SELECT COUNT(event_id) as total FROM event WHERE user_id =".$user_id);
$query_data = $query->fetch_assoc();
echo "<td><span style=font-weight:bold>". number_format($query_data['total'], 2) . "</span></td>";  ?>