Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[PHP] Count number of rows in table.

0 views
Skip to first unread message

Martín marqués

unread,
Aug 24, 2002, 10:33:20 AM8/24/02
to Tony Harrison, php-g...@lists.php.net
On Sáb 24 Ago 2002 11:19, Tony Harrison wrote:
> How would I count the number of rows in a mysql table with a WHERE clause?

select count(*) from table_name WHERE .....

Saludos... :-)

--
Porqué usar una base de datos relacional cualquiera,
si podés usar PostgreSQL?
-----------------------------------------------------------------
Martín Marqués | mmar...@unl.edu.ar
Programador, Administrador, DBA | Centro de Telematica
Universidad Nacional
del Litoral
-----------------------------------------------------------------

Tony Harrison

unread,
Aug 24, 2002, 5:54:09 PM8/24/02
to php-g...@lists.php.net
And do I need to use any other functions like mysql_fetch_row() ?

"Martín marqués" <mar...@bugs.unl.edu.ar> wrote in message
news:200208241133...@bugs.unl.edu.ar...

Matt

unread,
Aug 25, 2002, 9:05:50 AM8/25/02
to Tony Harrison, php-g...@lists.php.net
>>> On Sáb 24 Ago 2002 11:19, Tony Harrison wrote:
>>> How would I count the number of rows in a mysql table with a WHERE
>>> clause?
> >
>> "Martín marqués" <mar...@bugs.unl.edu.ar> wrote in message
> > news:200208241133...@bugs.unl.edu.ar...
> > select count(*) from table_name WHERE .....
> >

>From: "Tony Harrison" <ton...@tharrison21.fsnet.co.uk>
> Sent: Saturday, August 24, 2002 5:54 PM
> Subject: Re: [PHP] Count number of rows in table.
>
> And do I need to use any other functions like mysql_fetch_row() ?
>

Yes. It's a regular sql statement, so you must fetch the results.

$sql = "select count(*) from table_name WHERE .....";
$result = mysql_query($sql) or die(mysql_error);
$row = mysql_fetch_row($result);
$rowCount=$row[0];


Salamander

unread,
Aug 25, 2002, 10:07:17 AM8/25/02
to b...@startpunt.cc, Matt, Tony Harrison, php-g...@lists.php.net
or, you should be able to simply do this, without the cost of fetching
the results:

$result = mysql_query("SELECT count(*) FROM table WHERE");
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows\n";


>> $sql = "select count(*) from table_name WHERE .....";
>> $result = mysql_query($sql) or die(mysql_error);
>> $row = mysql_fetch_row($result);
>> $rowCount=$row[0];

> or


> $sql = "select count(*) from table_name WHERE .....";
> $result = mysql_query($sql) or die(mysql_error);

> $rowCount=mysql_result($result);
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Bas Jobsen

unread,
Aug 25, 2002, 8:41:23 AM8/25/02
to Matt, Tony Harrison, php-g...@lists.php.net
> $sql = "select count(*) from table_name WHERE .....";
> $result = mysql_query($sql) or die(mysql_error);
> $row = mysql_fetch_row($result);
> $rowCount=$row[0];
or
$sql = "select count(*) from table_name WHERE .....";
$result = mysql_query($sql) or die(mysql_error);
$rowCount=mysql_result($result);

Jason Wong

unread,
Aug 25, 2002, 10:11:46 AM8/25/02
to php-g...@lists.php.net
On Sunday 25 August 2002 22:07, salamander wrote:
> or, you should be able to simply do this, without the cost of fetching
> the results:
>
> $result = mysql_query("SELECT count(*) FROM table WHERE");
> $num_rows = mysql_num_rows($result);
> echo "$num_rows Rows\n";

No. This only returns 1 row regardless. "SELECT COUNT(*) FROM ..." only
returns a single row with a single column containing the row count. Using
mysql_num_rows() on that will always return 1.

--
Jason Wong -> Gremlins Associates -> www.gremlins.com.hk
Open Source Software Systems Integrators
* Web Design & Hosting * Internet & Intranet Applications Development *

/*
I'm going to Vietnam at the request of the White House. President Johnson
says a war isn't really a war without my jokes.
-- Bob Hope
*/

Salamander

unread,
Aug 25, 2002, 3:11:25 PM8/25/02
to php-g...@lists.php.net
okay, so then a "select *" and then a num_rows ...

Chris Shiflett

unread,
Aug 25, 2002, 3:24:13 PM8/25/02
to salamander, php-g...@lists.php.net
I think you were on the right track with your first response. Selecting
a count(*) gives you the number of rows returned without the overhead of
actually returning all of the rows to PHP. Most people will rename the
count as something more easy to reference (and/or more descriptive) in
the result set:

select count(*) as num_movies from movies where actor='Val Kilmer';

What Jason was trying to explain is that the data you are looking for is
actually in the query you performed. You specifically queried for the
number of rows. Your result set consists of a single row with a single
element, num_movies.

Happy hacking.

Chris

0 new messages