Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: How do I avoid division by zero error?

1. ## 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?

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

• 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

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

• ## 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)

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

• PHP Code:
``` if ( \$variable6 == 0) { \$variable6 = 1; } else { \$avgBuy = (\$variable5 / \$variable6); }  ```

• ## 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++;            } ```

• Yes your database has 0, so it's trying to times a number that can't be times.

• Originally Posted by masterofollies
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
•