This topic is locked

sum a column using PHP

1/21/2023 5:38:34 PM
PHPRunner General questions
francesco author

I have a snippet with this code to retrieve a sum of the column differenza:

$exc = DB::Query( "SELECT SUM(differenza) as totaleexc FROM exception" ) ;
$dataexc = $exc->fetchAssoc();
$oreexc= $dataexc[ 'totaleexc' ] ;

it seems correct but I have a : Fatal error: Uncaught Error: Call to a member function fetchAssoc() on bool

Can you help me?

Sergey Kornilov admin 1/22/2023

Make sure your SQL query is correct and returns data. If your SQL query returns no data you will be getting an error lie this.

francesco author 1/22/2023

the code is correct but the column differenza is a calculated field and it isn't stored in database, that's the problem. I'm finding an alternate way and I will post the result, thanks