j152 Posted June 19, 2008 Share Posted June 19, 2008 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) // 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) ?> Link to comment https://forums.phpfreaks.com/topic/110957-solved-why-division-by-zero-error-if-the-result-divided-by-isnt-zero-code-included/ Share on other sites More sharing options...
kenrbnsn Posted June 19, 2008 Share Posted June 19, 2008 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 Link to comment https://forums.phpfreaks.com/topic/110957-solved-why-division-by-zero-error-if-the-result-divided-by-isnt-zero-code-included/#findComment-569236 Share on other sites More sharing options...
abdfahim Posted June 19, 2008 Share Posted June 19, 2008 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. Link to comment https://forums.phpfreaks.com/topic/110957-solved-why-division-by-zero-error-if-the-result-divided-by-isnt-zero-code-included/#findComment-569237 Share on other sites More sharing options...
DarkWater Posted June 19, 2008 Share Posted June 19, 2008 Yes, abdbuet has it correct. mysql_query() returns a result resource, from which you extract data with other mysql_* functions. Link to comment https://forums.phpfreaks.com/topic/110957-solved-why-division-by-zero-error-if-the-result-divided-by-isnt-zero-code-included/#findComment-569244 Share on other sites More sharing options...
j152 Posted June 19, 2008 Author Share Posted June 19, 2008 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... Link to comment https://forums.phpfreaks.com/topic/110957-solved-why-division-by-zero-error-if-the-result-divided-by-isnt-zero-code-included/#findComment-569261 Share on other sites More sharing options...
j152 Posted June 19, 2008 Author Share Posted June 19, 2008 Nevermind... I added the ; Thanks! Link to comment https://forums.phpfreaks.com/topic/110957-solved-why-division-by-zero-error-if-the-result-divided-by-isnt-zero-code-included/#findComment-569272 Share on other sites More sharing options...
abdfahim Posted June 19, 2008 Share Posted June 19, 2008 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. Link to comment https://forums.phpfreaks.com/topic/110957-solved-why-division-by-zero-error-if-the-result-divided-by-isnt-zero-code-included/#findComment-569307 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.