Page 1 of 1

2 problems: update calculated field and sum of fields

Posted: Mon Jun 02, 2003 3:37 am
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?

Posted: Mon Jun 02, 2003 3:57 am
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`;

Posted: Mon Jun 02, 2003 3:58 am
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];

Posted: Mon Jun 02, 2003 5:03 am
by pama
great - works perfectly, thank you very much :)

Posted: Mon Jun 02, 2003 5:46 am
by []InTeR[]
That's what whe are here for....