masgas Posted July 15, 2006 Share Posted July 15, 2006 Hi everyone!I'm doing a small survey among colleagues from work, and I want to show the results in numbers. The question is, how can I turn the result from my db into numbers and make variables of them so I can divide them, turn them into percentages, etc...I have got this code:<?phpfunction L_afiliados (){ $con1 = mysql_connect('','','') or die ("Imposible conectar"); @mysql_select_db('encuesta') or die ("no se selecciono bd"); $sql = "SELECT count(p2) FROM datos"; $total = mysql_query($sql); $totalrows = mysql_fetch_row($total); echo $totalrows[0]; }L_aifiliados ();?>It prints correctly the amount of rows in the field, but if I want to divide it by another number it gives me an error message (divided by zero)... I wish I could have the amount of people who answered yes and dived it by the total amount of people for instance, but I am not sure how to... Thank you in advance!! Quote Link to comment https://forums.phpfreaks.com/topic/14708-stats-from-data-base/ Share on other sites More sharing options...
pixy Posted July 15, 2006 Share Posted July 15, 2006 Try replacing the part of your script with this...[code]<?php$sql = "SELECT count(p2) FROM datos";$total = mysql_query($sql);if (mysql_num_rows($total) > 0) { $row = mysql_fetch_array($total, MYSQL_NUM); echo $row[0];}else { echo 'There are no records to count.';}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14708-stats-from-data-base/#findComment-58674 Share on other sites More sharing options...
akitchin Posted July 15, 2006 Share Posted July 15, 2006 that won't solve the division issue. how are you counting the number of total responses? you get a "divide by zero" error likely because the variable you're trying to divide by is unset (or 0). Quote Link to comment https://forums.phpfreaks.com/topic/14708-stats-from-data-base/#findComment-58676 Share on other sites More sharing options...
masgas Posted July 15, 2006 Author Share Posted July 15, 2006 I divided:$result = (L_afiliados/22);echo $result;this brought an error!I also tried:...@mysql_select_db('encuesta') or die ("no se selecciono bd"); $sql = "SELECT count(p2) FROM datos"; $total = mysql_query($sql); $totalrows = mysql_fetch_row($total); $res = $totalrows[0]; ... ...$res1 = ($res/22);but same result!how can I make a variable from the number of rows? or is it not possible with my query as it is? Quote Link to comment https://forums.phpfreaks.com/topic/14708-stats-from-data-base/#findComment-58689 Share on other sites More sharing options...
akitchin Posted July 15, 2006 Share Posted July 15, 2006 you should be able to grab the total number of rows as you are now, with a COUNT() statement. a few things you can do to narrow down the problem:1. add an or die() clause to the query:[code]$total = mysql_query($sql) or die(mysql_error());[/code]2. echo what you have in $res before running the division.what error in particular are you getting? divide by zero? that doesn't make much sense, since you're dividing by a constant (22). Quote Link to comment https://forums.phpfreaks.com/topic/14708-stats-from-data-base/#findComment-58701 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.