|
-
Jun 10th, 2008, 09:37 AM
#1
Thread Starter
Lively Member
unable to update table
Hello,
PHP Code:
$getsum1 = @mysql_query('select sum(product) as summ from tempo'); // calculating sum of values in a column named product
$row = mysql_fetch_array($getsum1); // fetching the result into an array
$sumtot = $row['summ']; // the result, which is number in decimals is returned as a string e.
I need this value for additional mathematical operation, but $sumtot is storing the value as string, hence I have to convert it into double.
Instead of
PHP Code:
$sumtot = $row['summ'];
I use
PHP Code:
$sumtot = doubleval($row['summ']);
echo (var_dump($sumtot)); // confirms that $sumtot holds a double value
PHP Code:
$finq = @mysql_query('update tempo set percent = (product*100)/$sumtot'); // using the sumtot to calculate percentage and update the same
This query is failing, the fields are not getting updated, what is wrong?
-
Jun 10th, 2008, 01:20 PM
#2
Lively Member
Re: unable to update table
Unless I'm mistaken, SELECT SUM() will not return an array.
How about:
Code:
$getsum1=mysql_query('select sum(product) as summ from tempo');
$sumtot=mysql_result($getsum1,0);
$finq=mysql_query('update tempo set percent=(product*100)/'.$sumtot);
-
Jun 10th, 2008, 02:11 PM
#3
Thread Starter
Lively Member
Re: unable to update table
That did it! wonderful, thank you.
Actually, i was getting the result [of sum(product)] in array and checked for its presence too, but using mysql_result was the best option (as u suggested).
i making a big mistake in my sql statement, see the difference
your correct way:
PHP Code:
$finq=mysql_query('update tempo set percent=(product*100)/'.$sumtot);
my incorrect way:
PHP Code:
$finq=mysql_query('update tempo set percent=(product*100)/$sumtot');
That made a lot of difference. i should have kept $sumtot out of single quote.
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|