2 problems: update calculated field and sum of fields

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
pama
Forum Newbie
Posts: 12
Joined: Fri May 30, 2003 8:37 am

2 problems: update calculated field and sum of fields

Post by pama »

hi,
i've got two problems:
1. i have calculated field in my form:

Code: Select all

$a = $a //$a is from mysql
$b = $b //$b is from mysql
$c = $c //$c is from mysql

$d = $a * $b * $c
echo $d; 
	 while ($row = mysql_fetch_row($mysql_result)) {
      if ($rowї0] == $d) {
	  $query = "UPDATE table SET $d";
where field in database is not updated, i don't know why....
2. i'd like to display sum of any column...

Code: Select all

$query = "SELECT SUM(column_name) FROM table" ;
		$mysqlresult = mysql_query($query);
		echo $mysqlresult;
but on the page i have "Resource id #9"...
what i'm doing wrong?
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

First problem:

Code: Select all

UPDATE `table` SET `somefield`='$d' WHERE `field`='value';
But i think where your looking for is done in one mysql statement.

Code: Select all

UPDATE `table` SET `somefield` = `a`*`b`*`c` WHERE `firstfield` = `a`*`b`*`c`;
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

The value that mysql_query returns is a resource handler. A kind of pointer.

What you are looking for is:

Code: Select all

$query = "SELECT SUM(column_name) FROM table" ; 
$mysqlresult = mysql_query($query); 
$result = mysql_fetch_array($mysqlresult);
echo $result[0];
pama
Forum Newbie
Posts: 12
Joined: Fri May 30, 2003 8:37 am

Post by pama »

great - works perfectly, thank you very much :)
[]InTeR[]
Forum Regular
Posts: 416
Joined: Thu Apr 24, 2003 6:51 am
Location: The Netherlands

Post by []InTeR[] »

That's what whe are here for....
Post Reply