Forum Moderators: coopster

Message Too Old, No Replies

How to print count of different values from one column?

         

shams

2:23 am on Dec 9, 2006 (gmt 0)

10+ Year Member



hi,
there is mysql table cs, column name is Z, the vlues are S and R:
+------+
¦ Z ¦
+------+
¦ R ¦
¦ R
S
R
S ¦
+------+
there should be variable:
$var = 'Z';
how to print the values of Z with php using the $var as:
there are 3 R in Z
there are 2 S in Z

this is my query but not working:
<?php
include 'library/config.php';
include 'library/opendb.php';
$var = 'Z';
$query = "SELECT $var,Count($var) FROM cs GROUP By $var";

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){

echo " There are ". $row['Count($var)']." in " .$var." patients";
echo "<br />";
}
include 'library/closedb.php';
?>

justageek

12:50 pm on Dec 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

<?php
include 'library/config.php';
include 'library/opendb.php';
$var = 'Z';
$query = "SELECT $var,Count($var) as numpats FROM cs GROUP By $var";

$result = mysql_query($query) or die(mysql_error());

// Print out result
while($row = mysql_fetch_array($result)){

echo " There are ". $row['numpats']." in " .$var." patients";
echo "<br />";
}
include 'library/closedb.php';
?>

Assuming your $var is Z in the query that should get you the number you want.

JAG