2017-08-04 118 views
1

我明白了總數。但它包括所有用戶。我希望它僅通過登錄用戶顯示總金額。請幫幫我!我的總和(總計)顯示了所有用戶的總和

<?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>"; 
    } 
?> 
+0

'SELECT total FROM event WHERE user_id = LOGGED_IN_USER_ID' –

+0

什麼是'事件'表結構?你如何識別這個表中的用戶? – alpadev

+0

用戶由用戶名標識。 $ _SESSION [ '用戶名'。結構是entryID,artistName,場地,eventDate,用戶名,座位,價格,數量,總數。 – kidrawk

回答

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>"; 
    } 
?> 
+0

感謝您的回覆。不幸的是它不起作用.. – kidrawk

+0

嘗試在此查詢中更新字段名稱並運行到phpmyadmin,檢查結果: 'SELECT COUNT(event_id)as total FROM event WHERE user_id = 5' –

+0

我得到了這個警告:提供了無效參數foreach() – kidrawk

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>"; ?> 
+0

嘿謝謝你的回覆。我得到了這個 - >致命錯誤:未捕獲錯誤:調用成員函數fetch_assoc()布爾 – kidrawk

+0

嗨,你可以請顯示你的表結構.. 當你使用錯誤的列名稱,我們會得到這個錯誤。 或者某些項目在db中找不到 –