Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2011
    Posts
    5

    Very novice question

    Hi guys. I really shouldn't be messin' with this stuff at my level of knowledge, but ... I'm trying to round the following query result to 2 dec places. Typical output is currently 8.14000002 and I'm after 8.14

    Here's my code so far - hope it's not too bad!

    $connection = mysql_connect($host,$user,$password) or die("Could not connect: ".mysql_error());
    mysql_select_db($database,$connection) or die("Error in selecting the database:".mysql_error());

    $query = "SELECT SUM(product_price)/100*10 FROM ps_order_detail WHERE product_supplier_reference = 'test-sch-ch-hood-2011' or product_supplier_reference = 'test-sch-girl-blazer-2011'"; //add supplier references

    $result = mysql_query($query) or die(mysql_error());

    // Print out result
    while($row = mysql_fetch_array($result)){
    echo "Total cash back to school so far ". $row['SUM(product_price)/100*10'];
    echo "<br />";

    }

    ?>

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by millers View Post
    ... I'm trying to round the following query result to 2 dec places.
    may i suggest using the ROUND function

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Feb 2011
    Posts
    5

    Can't get the syntax right

    Hi, thanks ..

    I've tried: SELECT ROUND(SUM(product_price)/100*10,2)...
    in the query but that just breaks the 'print out result'. I get ....
    Do I have to alter that section also?

  4. #4
    Join Date
    Mar 2003
    Location
    The Bottom of The Barrel
    Posts
    6,102
    Are you sure you wanted to divide by 1000?
    oh yeah... documentation... I have heard of that.

    *** What Do You Want In The MS Access Forum? ***

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by Teddy View Post
    Are you sure you wanted to divide by 1000?
    he's dividing by 10, teddy

    dividing by 1000 would be foo/(100*10)

    millers, you should use a column alias in your query, it will make displaying the result simpler
    Code:
    SELECT ROUND(...) AS rounded ...
    
    echo "Total cash back to school so far  ". $row['rounded'];
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  6. #6
    Join Date
    Feb 2011
    Posts
    5

    Novice looking for help

    I admit I'm a complete novice. That's why I'm here looking for help. This is a bit of a one-off for me and I'm not planning on becoming an expert. I've looked at the documentation, but just can't seem to get the syntax right.

    And, no! I don't want to divide by 1000 - I thought I was taking 10% off!!

  7. #7
    Join Date
    Feb 2011
    Posts
    5

    Thanks

    OK, many thanks guys, I'll give it my best ..

  8. #8
    Join Date
    Feb 2011
    Posts
    5
    Thanks r937 --- It worked

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •