MasterJoe
MasterJoe

Reputation: 29

php sum column based on year criteria

I'm trying to sum a column named "total_fee" where the payment date is equaled to the current year but I received a blank. The code works with no error but the query came up empty. The payment_date is a datetime/timestamp. Does anyone know the php statement to sum current year and last year total? I have tried various ways with no lucks.

$stmt2 = $DB_CON_C->prepare("SELECT SUM(total_fee) AS current_year
                             FROM `".$row['email']."`
                             WHERE payment_date = date('Y')");
$stmt2->execute();
$sum2 = $stmt2->fetch(PDO::FETCH_ASSOC);
echo '<td>' .$sum2['current_year'].'</td>'

Upvotes: 0

Views: 53

Answers (1)

Vbudo
Vbudo

Reputation: 370

You need to supply the date value, not a php expression for the date.
You are also using prepared statements incorrectly. Look here https://www.w3schools.com/php/php_mysql_prepared_statements.asp

An easy way to test your query is to use 'between'

... WHERE payment_date BETWEEN '2017-01-01' AND '2017-12-31'

Upvotes: 1

Related Questions