Jump to content


Photo

stats from data base


  • Please log in to reply
4 replies to this topic

#1 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 15 July 2006 - 09:16 PM

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:

<?php

function 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!!


#2 pixy

pixy
  • Members
  • PipPipPip
  • Advanced Member
  • 295 posts

Posted 15 July 2006 - 09:21 PM

Try replacing the part of your script with this...
<?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.';
}
?>

This is a .44 Caliber Loveletter straight through my heart.

Tabulas + Threadless + Hire Me!


#3 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 15 July 2006 - 09:22 PM

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

#4 masgas

masgas
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 15 July 2006 - 09:47 PM

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?



#5 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 15 July 2006 - 10:28 PM

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:

$total = mysql_query($sql) or die(mysql_error());

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users