SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2006
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Loop Problem,plz help

    $totalCost=0;
    while($row = $result->fetch_assoc())
    {

    $totalCost+=$row["price"];
    --------------------------
    Why $totalcost =0?what happen?

  2. #2
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    many reasons.
    the loop didn't run at all, possibly because there were no records in the resultset.
    the price field do not have any values or have 0 as its values.
    if negative prices are also allowed in price, the positive and negative values cancelled each other out.

  3. #3
    SitePoint Enthusiast fredanthony's Avatar
    Join Date
    Aug 2006
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Where' the rest of the code?

    PHP Code:
    $query "SELECT * FROM tablename";

    if (
    $result $mysqli->query($query)) {

       
    /* fetch associative array */
       
    while ($row $result->fetch_assoc()) {
           
    $totalCost+=$row["price"];
       }

       
    /* free result set */
       
    $result->close();



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
  •