SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jul 2004
    Location
    Telford
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with numeric value extracting as string

    Hi folks,
    I have an SQL retrieve from a database which is working fine. One part of it goes like this:

    PHP Code:
    ... SUMreviews.review_rating ) / COUNTproducts.product_id ) AS review_rating... 
    When I run the SQL query in PHPMyAdmin, the results are returned fine as numeric values with 2 decimal places.
    In the code, I do the following:

    PHP Code:
        while ( $row mysql_fetch_assoc($result) ) {
          
    $review_rating $row["review_rating"]; 
    I would expect $review_rating to have a value of, for example, 7.00 or 8.50.
    But it's a string with value 7 or 9.
    Can anyone point me in the direction of this error, please?
    PHP Version 4.3.11
    MySQL 4.0.21
    Thanks.
    Last edited by Spectre; May 8, 2005 at 12:17.

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm not sure it'll work, but try using type casting:

    PHP Code:
    $review_rating = (float) $row['review_rating']; 
    If that doesn't work, try the following:

    PHP Code:
    $review_rating number_format($row['review_rating'], 2); 
    ERIK RIKLUND :: Yes, I've been gone quite a while.


Bookmarks

Posting Permissions

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