Jump to content

[SOLVED] Why Division by Zero Error if the result divided by isn't zero? - Code Included


j152

Recommended Posts

Hi All..

 

I continue getting a division by zero error, but the # I'm dividing by isn't zero....

 

Versions  Mysql 5 & PHP 5

 

I am receiving a value from a form                  $pollen_density = 500,000 (or whatever is entered)

I'm attempting to count the rows in my table    $result                          (currently 10 rows)

and divide input by row count                        $pollen_density / $result = $reduced_pollen_count

 

Below is my code....

 

I have worked out the script, so that it doesn't return any error messages, so I think the syntax is

 

Line 9 should be a value of 10

Line 10 Producing Error Message ... but it should have a value above 0 because the rows = 10, and whatever data captured by the form is also above 0

Line 25 should set the table data to    $reduced_pollen_count  & update the table data if it meets the condition set by WHERE

 

 

But I'm doing something wrong.... any analysis  would be greatly appreciated....

 

<?php

# ----------------------------------------------------

 

1)    <?php

2)    include 'config.php';

3)    // Receiving variables from air data form

4)

5)    @$pollen_density                           = addslashes($_POST['pollen_density']);

6)

7)

8)    // Query the database and calculate new pollen count

9)    $result = mysql_query("SELECT COUNT (*) FROM pollution");

10)  $reduced_pollen_count = $pollen_density / $result;

11)

12)

13)  // verifies input = double checked against hardcopy data

14)

15)  if (strlen($approved) == 0 )

16)

17)  {

18)  die("<p align='center'><font face='Arial' size='3' color='#FF0000'>Double check your notes!  Verify 19)  that you have copied everything correctly!!!  Geez!  Wake up already! Do you want everyone to get 20)  caught up in a frenzy of sneezing?!!</font></p>");

21)  }

22) 

23)  //saving record in a MYSQL Database

24)

25)  $query = "UPDATE pollution SET cleanair = $reduced_pollen_count WHERE sample_data = 200,000";

26) 

27) 

28)  if ( $air_data != 200,000)

29)  {

30)  die("<p align='center'><font face='Arial' size='3' color='#FF0000'>REDO!</font></p>");

31)  }

32) 

33)  echo("<p align='center'><font face='Arial' size='3' color='#FF0000'> SUCCESS!  The data has been 34)  updated, and the ENTIRE WORLD IS POLLUTION FREE.</font></p>");

35)   

36)    include 'close_db.php';

37)    ?>

 

 

 

This line:

<?php
$result = mysql_query("SELECT COUNT (*) FROM pollution");
?>

does not give you the number, it returns a pointer into the dataset that contains the number.

 

You want to do something like:

<?php
$q = "select count(*) as cnt from pollution";
$rs = mysql_query($q);
$rw = mysql_fetch_assoc($rs);
$reduced_pollen_count = $pollen_density / $rw['cnt'];
?>

 

Ken

who said Line 9 should be a value of 10? line9 is a php result array, it has no straight forword value. To get the row count, you have to write

 

$result = mysql_query("SELECT * FROM pollution"); //this line return an array which u have to process
$num_row=mysql_num_rows($result);  //This line return 10

 

Hope, this will solve the problem.

 

Please avoid those multi spaces between words when posting. It becomes harder to read then.

Thanks for the help, but I entered....

 

$q = "SELECT COUNT (*) as cnt FROM pollution";

$result = mysql_query($q);

$num_row=mysql_num_rows($result);

$rw = mysql_fetch_assoc($result);

$reduced_pollen_count = $pollen_density / $rw['cnt'];

 

which produced the error messages

 

** Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource

** Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

** Warning: Division by zero blah blah blah

 

and I entered

 

$result = mysql_query("SELECT * FROM pollution")

$num_row = mysql_num_rows($result);

$reduced_pollen_count = $pollen_density / $num_row;

 

Which seemed to work better except I got a

 

** Parse error: syntax error, unexpected T_VARIABLE

 

Thanks for any assistance...

 

 

 

 

 

 

just a simple but very useful tips .. instead of using only

$result = mysql_query($q);

you better use

$result = mysql_query($q) or die(mysql_error());

always. It will help you to know whether there is any error occurs in your SQL statement.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.