This topic is locked
[SOLVED]

 Is addition supported in queries?

1/12/2007 10:39:19 AM
PHPRunner General questions
L
larsonsc author

I'm trying to create a calulated field that totals three fields and displays the total for each record at the end of the row. The code I am using is shown below, and I have tried several different versions of it, but I always just get a blank field in my report. Is this supported or is my lack of familiarity with PHP and SQL causing me an issue again? Sorry if this topic has been addressed previously, I tried several searches last night and could not locate a solution. Thanks.

(`Adoption_Fee_1` + `Adoption_Fee_2` + `Adoption_Fee_3`) AS "Total Adoption Fee"
T
thesofa 1/12/2007

you answered this yourself in THIS post not so long ago, I think you may need to remove the brackets and check that the field types are the same in the database.

I just set up a quick table od 4 fields, a,b,c,d with random figures in each, each field was and integer.

I did a quick sql query like this

select `a`,

`b`,

`c`,

`d` ,

`a`+`b`+`c`+`d` AS Weer

From `sums`



when I built the page it worked, so i tried it with brackets like you have, and it still worked.

I put no quotes around the alias.

HTH

Sergey Kornilov admin 1/12/2007

In MySQL use backticks to wrap field names:

`Adoption_Fee_1` + `Adoption_Fee_2` + `Adoption_Fee_3` AS `Total Adoption Fee`
L
larsonsc author 1/12/2007

As it turns out it is related to two of the monetary fields being set to "NULL". I edited the fields to "NOT NULL" and created a default value of 0.00 for the fields that are not required to be filled (since we don't always have 3 forms of payment for an adoption). The addition is working fine now.
I guess since my report was an afterthought to the initial development, the "NULL" slipped my melon. I kept thinking if the field was null, the math would just do something like add a zero value. How soon I foget that null hoses up so many things.

T
thesofa 1/12/2007

It has caught me out loads of times.

D
Dale 1/12/2007

Just a thought. I found it easiest to control everything from one place. So I make all my fields defined as Null.

Then I just control the required with PHPRunner.
I got caught once defining it as NOT NULL with a 0.00. Then when saving a record with a no value for the number on it would not save and just throw an error. CANT SAVE value is NULL. I found PHPRunner returns a NULL to the database if there is no value in the form field.
So only my primary key is NOT NULL.
I dont know if this is the best way, but hey, stopped those errors, and it is nice to use PHPRunner to take care of the required and formating.
Just a thought.