How i can view sum for the column (Collections) where (userid = $userid) ?
Example : I need to view the result for user id = 2, Collections = 200 + 330 = 530, I need to view this result (530)
My Table
------------------------------
| id | user_id | Collections |
------------------------------
| 1 | 2 | 200 |
------------------------------
| 2 | 2 | 330 |
------------------------------
| 3 | 7 | 120 |
------------------------------
| 4 | 8 | 760 |
------------------------------
| 5 | 9 | 200 |
------------------------------
| 6 | 9 | 100 |
------------------------------
My Code
<?php
$user_id = get_current_user_id();
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "dbname";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$query = "SELECT SUM(Collections) FROM invoices where user_id = $user_id";
$result = mysql_query($query) or die(mysql_error());
// Print out result
?>
- I am a beginner in php & mysql