Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    North East England
    Posts
    853
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How do I avoid division by zero error?

    Warning: Division by zero in...

    I'm trying to calculate a percentage using...

    $percent1 = round(($num_rows1*100)/$num_rows_total);

    $percent2 = round(($num_rows2*100)/$num_rows_total);

    and so on...

    what's causing this error and what can I do to prevent it?
    [+] Computer/PC issues [+] Silverpaw3D
    ------------------------------------------------
    Never buy a dwarf with learning disabilities...

    ...it's not big, and it's not clever.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Well that's easy.....stop dividing by zero.

    Ok well if your num_rows_total variable is equal to zero then obviously you will get this error. Are these rows referring to records in a database?

    You could put a conditional statement in there:

    if($num_rows_total!=0)
    $percent1 = round(($num_rows1*100)/$num_rows_total);
    $percent2 = round(($num_rows2*100)/$num_rows_total);
    }
    else{
    //do something else if it is zero
    }
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    North East England
    Posts
    853
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hold on!

    There's got to be rows in order to call this expression so I should never see this error

    ....

    okay I've sorted it simply by moving the bit that counts total rows ahead of the bit using the resulting variable, so now I'm thinking php works down the page?

    Thatks for the quick response anyhoo
    [+] Computer/PC issues [+] Silverpaw3D
    ------------------------------------------------
    Never buy a dwarf with learning disabilities...

    ...it's not big, and it's not clever.

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    just like any language the coding will be read left to right and top to bottom unless unless you put coding into functions/methods/subroutines etc.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    what if the number returned from a row is 0?

    Glad to find this post and I hope you can assist me.

    I'm building an application where the a row I return many times may be 0.

    PHP Code:
    $query mysql_query("SELECT * FROM Azgalor_Alliance WHERE $metode LIKE '%$search%' LIMIT 0, 50");
    while (
    $row = @mysql_fetch_array($query))
    {
    $variable1=$row["Item_Desc"];
    $variable2=$row["Enchant"];
    $variable3=$row["Item_Class"];
    $variable4=$row["Min_Price"];
    $variable5=$row["Total_Price"];
    $variable6=$row["Buy_Count"];
    $avgBuy = ($variable5 $variable6); 
    how can I go about making $variable6 = 1 if it returns 0 ?

    (this is for an auction system, Total_Price is the sum of all buy ammounts of an item and Buy_Count is the number of times the item has been purchased on the auction)

    Thanks in advance...

    I figured an if statement would work... but I'm lost on how I should write it.

  • #6
    New to the CF scene
    Join Date
    Apr 2006
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    if ( $variable6 == 0) {
    $variable6 1;
    } else {
    $avgBuy = ($variable5 $variable6);

    Works... if there is a better way please advise me!

  • #7
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Warning: Division by zero in /index.php line 103

    this is the code showing error

    Pasting a part of it
    PHP Code:
    <?php



    $result 
    mysql_query("SELECT * FROM news  where section = 'news' ORDER BY id DESC LIMIT 0 , 4 ");

                

                
    $i 0;

                
    $news_id    = array();
                
    //(Line 103 Comes here )
                
    $news_title    = array();

                
    $news_content    = array();

                

                while(
    $row mysql_fetch_array($result))

                  {

                      
    $news_id[$i]     = $row['id'];

                      
    $news_title[$i]     = $row['title'];

                    
    $news_content[$i]     = $row['content'];

                    

                    
    $i++;

                }
    Last edited by Inigoesdr; 10-04-2008 at 04:16 AM.

  • #8
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Yes your database has 0, so it's trying to times a number that can't be times.

  • #9
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by masterofollies View Post
    Yes your database has 0, so it's trying to times a number that can't be times.

    How Can I solve it sir


  •  

    Posting Permissions

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