Results 1 to 3 of 3

Thread: unable to update table

  1. #1

    Thread Starter
    Lively Member sridharao's Avatar
    Join Date
    Feb 2007
    Posts
    106

    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?

  2. #2
    Lively Member
    Join Date
    Jul 2004
    Posts
    121

    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);

  3. #3

    Thread Starter
    Lively Member sridharao's Avatar
    Join Date
    Feb 2007
    Posts
    106

    Talking 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
  •  



Click Here to Expand Forum to Full Width