...

View Full Version : How do I avoid division by zero error?



mouse
07-20-2002, 10:47 PM
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?

Spookster
07-20-2002, 10:56 PM
Well that's easy.....stop dividing by zero. :D

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
}

mouse
07-20-2002, 11:06 PM
Hold on! :confused:

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

....

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? :o:rolleyes:

Thatks for the quick response anyhoo :thumbsup:

Spookster
07-21-2002, 12:23 AM
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. :)

foxy
04-14-2006, 06:11 AM
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.


$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.:confused:

foxy
04-14-2006, 06:21 AM
if ( $variable6 == 0) {
$variable6 = 1;
} else {
$avgBuy = ($variable5 / $variable6);
}

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

nithin.sha
10-02-2008, 11:56 AM
this is the code showing error

Pasting a part of it


<?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++;

}

masterofollies
10-02-2008, 02:44 PM
Yes your database has 0, so it's trying to times a number that can't be times.

nithin.sha
10-03-2008, 09:50 AM
Yes your database has 0, so it's trying to times a number that can't be times.


How Can I solve it sir



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum