php3-list | 2000051
Date: 05/15/00
- Next message: Matt Allen: "Re: [PHP3] Damn it !!!!"
- Previous message: Tyler Longren: "Re: [PHP3] Damn it !!!!"
- In reply to: usha: "[PHP3] COUNT(*)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
In article <000d01bfbe36$fa6b2320$90e6c5cb <email protected>>, usha <email protected>
("usha") wrote:
> $myquery =3D"SELECT * FROM $table where category =3D '$category' and =
> archive =3D '$archive'";
> $result2 =3D mysql_query($myquery ,$db);
> $totalrows =3D mysql_num_rows($result2);
>
> Then $totalrows displays 5
Yes, you asked for five rows, and you got five rows.
> But if I code like this
>
> $myquery =3D"SELECT COUNT( *) FROM $table where category =3D '$category' =
> and archive =3D '$archive'";
> $result2 =3D mysql_query($myquery ,$db);
> $totalrows =3D mysql_num_rows($result2);
>
> Then I am not able to access COUNT(*). Also $totlarows does not hold any =
> value. Please tell me why
$totalrows should always be 1. This is because count(*) always returns
exactly one row of data: The row containing the count (totalrows) of all
the rows.
In other words, MySQL has added up the answer already, and returned the
answer as a single row of data.
To get the answer, since MySQL is now doing the counting for you instead
of PHP (and this is good cuz MySQL can count database rows way faster than
PHP can), you'll need something like this:
$totalrows = mysql_result($result2, 0, 0);
Or this:
list($totalrows) = mysql_fetch_row($result2);
Or this:
$row = mysql_fetch_array($result2);
$totalrows = $row[0];
You'd think that this would work:
$row = mysql_fetch_array($result2);
$totalrows = $row['count(*)'];
but it probably won't since 'count(*)' is a pretty funky name for a
column. You could change your SQL to rename count(*) as something else
though:
$myquery = "select count(*) as totalrows from ...";
.
.
.
$row = mysql_fetch_array($result2);
$totalrows = $row['totalrows'];
-- Richard Lynch | If this was worth $$$ to you, buy a CD US Customer Support Director | from one of the artists listed here: Zend Technologies USA | http://www.L-I-E.com/artists.htm http://www.zend.com | (this has nothing to do with Zend, duh!)-- PHP 3 Mailing List <http://www.php.net/> To unsubscribe, send an empty message to php3-unsubscribe <email protected> To subscribe to the digest, e-mail: php3-digest-subscribe <email protected> To search the mailing list archive, go to: http://www.php.net/mailsearch.php3 To contact the list administrators, e-mail: php-list-admin <email protected>
- Next message: Matt Allen: "Re: [PHP3] Damn it !!!!"
- Previous message: Tyler Longren: "Re: [PHP3] Damn it !!!!"
- In reply to: usha: "[PHP3] COUNT(*)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

