I want to get the sum of the values in a column I ten print. I am executing the PHP code in the /devel/php page from the Devel module.
The query I want to execute is SELECT SUM(column_name) FROM table_name
.
I tried the following code, but I get a Call to undefined function db_result() error.
$val = db_result(db_query('SELECT SUM(column_name) FROM {table_name}'));
print $val;
I also tried the following code, but I get a Object of class DatabaseStatementBase could not be converted to string in eval() error.
$query = db_select('table_name');
$query->addExpression('sum(column_name)', 'total');
$result = $query->execute();
print $result;
If I do not print $result
, it works and executes, but I want to print the value.
How can I achieve this?