Jump to content

stats from data base


masgas

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!!
Link to comment
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]
Link to comment
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?

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.