Re: best way to get COUNT from result set
| From: | Philip Olson | Date: | Wed, 20 Dec 2000 01:02:51 +0000 |
| Subject: | Re: best way to get COUNT from result set | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-31111@lists.php.net to get a copy of this message | ||
Hey, try:
function getRowCount ($db_table,$db_column='',$db_value='')
{
$q = "select count(*) from $db_table";
if (!empty($db_column)) {
$q .= " WHERE $db_column='$db_value'";
}
$r = mysql_query ("$q") or die (mysql_error());
Return mysql_result($r,0);
}
Uses :
------
// Returns total number of rows in table
getRowCount('tablename');
// Returns number of NULL (empty) rows in a table column
getRowCount('tablename','columnname');
// Returns number of VALUES ($db_value) in table column
getRowCount('tablename','columnname','value');
Note :
-------
mysql_num_rows() makes more sense if you've already done the SELECT and
need to know how many rows are in your result set.
Regards,
Philip
--.sig
[ Want to write Good code? First, write Bad code. ]
[ Will work for Money : http://www.cornado.com/ ]
On Tue, 19 Dec 2000, Christian Dechery wrote:
> there's no way to get a single result from a query (since several functions
> like MAX(), AVG(), COUNT(), ...) return single values...
>
> I could go like...
>
> $q = "select count(id) from table";
> $rslt = mysql_query($q);
> $count = mysql_fetch_row($rslt);
>
> and then $count[0] would have what I want...
> but this is kinda "syntax ugly"... I only want ONE value... I don't want an
> ARRAY...
> of course $count = $count[0] (I'm sure a lot of people would sugest that)
> is possible, but is also "syntax ugly"...
>
> is there any way around this?
> ____________________________
> . Christian Dechery (lemming)
> . http://www.tanamesa.com.br
> . Gaita-L Owner / Web Developer
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>