Jump to content

Archived

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

masgas

stats from data base

Recommended Posts

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

×

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.