1

in drupal 7, i want to get the sum of a column in the database and display it using a static query and show the display in a block. I am executing the php code at site.com/devel/php from the devel module.

Plese do note that I am not proficient in coding, and i am a script kiddie. I am not a developer, and i have only used the drupal platform as a designer, and have only dealy with the configurations and menus.

the query i want to exectue is very very simple. it is

SELECT SUM(column_name) FROM table_name

i have tried using static query as follows:

$val=db_result(db_query('SELECT SUM(column_name) FROM {table_name}'));
print $val;
then i get --- *"Fatal error: Call to undefined function db_result() "*

i also tried

$query = db_select('table_name');
$query->addExpression('sum(column_name)', 'total');
$result = $query->execute();
print $result;

then i get --- "Recoverable fatal error: Object of class DatabaseStatementBase could not be converted to string in eval()"

I want to print the value of the total sum. If i remove the print statement, then it works and executes. but i want the value printed. as in, if i only execute the first three lines

$query = db_select('table_name');
$query->addExpression('sum(column_name)', 'total');
$result = $query->execute();

it runs successfully, but with the print statement it does not.

I am executing the php code at site.com/devel/php. I intend to use this result that gets printed either in a page or in a block.

Is there any noob mistake that i have made, and can you guys guide me to the right resource to get thhis to work.

I have looked at issues that other prople have had and have been copying thaat and modifying it so it used the table i want to use and the column names within it.

Truth be told, I dont know if we cam print variables when we are executing php code in drupal. if i run the query with a print 'Hello', then it prints. So i know that the print statement works, but i am not sure if it prints variable. If it is not possible to use the SUM in the query, will i have to fetch rows individually using for each, and within the loop add the values to a sum variable to get the total? Also, in a few of the posts i read that db_quey()/some other function is not there in d7 and was a part of d6. So i am unsure if i am even going in the right direction.

Any help here would mean the world to me.

Regards and Peace from the UAE,

Aamir

0

The first error is caused from the fact that Drupal 7 doesn't define db_result() anymore; it was used from Drupal 6.

The second error is caused from $query->execute() returning an object of that class, which you cannot convert into a string (which is what you do when you try to print it). You should use code similar to the following code.

$query = db_select('table_name');
$query->addExpression('SUM(column_name)', 'total');
$result = $query->execute()->fetchField();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.