A simple Query?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

A simple Query?

Post by Alpal »

Am trying to write a query, seems simple enough
SELECT SUM(members.Under_15_Vic) + SUM(members.Under_12_Vic) + SUM(members.Under_18_Vic)
FROM members
All I want to do is get a total of the 3 columns, it works providing all the columns have a value.
If a column has no values entered (all entries are null) then the SUM ceases to work?
The columns represent payment for junior tournaments, column type is set as Numeric, tinyint
The outcome of the query is being used as part of a summary page, Is there a way I can then Insert the outcome into another table?
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Re: A simple Query?

Post by Rovas »

Why don' t you use:

Code: Select all

//short version code to get you started
  $arraySumColumns[]=mysqli->query( "SELECT Under_15_Vic, Under_12_Vic, Under_18_Vic FROM members;");
  $sum=array_sum($arraySumColumns);
it' s much faster.
Alpal
Forum Commoner
Posts: 39
Joined: Mon Jul 26, 2010 4:08 am

Re: A simple Query?

Post by Alpal »

I guess it is obvious that I am a newbie to PHP. I use dreamweaver and have created a database, it is working fine. Have not used arrays before, spent most of the day doing online tutorials but still cannot get your code to work

Tried some simple arrays and they work
// Test 1 Output =45
$a=array(0=>"5",1=>"15",2=>"25");
echo array_sum($a);

// Test 2 Output =9.3
$value2= array(1.5,2.3,5.5);
echo array_sum($value2);

Tried your code.
$VJSinc[]=mysqli->query( "SELECT Under_15_Vic, Under_12_Vic, Under_18_Vic FROM members;");
$sum=array_sum($VJSinc);
echo array_sum($VJSinc);

And got this error message
Parse error: syntax error, unexpected T_OBJECT_OPERATOR

Thankyou for your reply any assistance would be greatly appreciated.
Post Reply