Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 260199FBBE1 for ; Wed, 28 Mar 2007 18:31:45 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 89671-01 for ; Wed, 28 Mar 2007 18:31:41 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.4 Received: from nf-out-0910.google.com (nf-out-0910.google.com [64.233.182.187]) by postgresql.org (Postfix) with ESMTP id D73839FBBCF for ; Wed, 28 Mar 2007 18:31:40 -0300 (ADT) Received: by nf-out-0910.google.com with SMTP id x4so3809742nfb for ; Wed, 28 Mar 2007 14:31:39 -0700 (PDT) DKIM-Signature: a=rsa-sha1; c=relaxed/relaxed; d=gmail.com; s=beta; h=domainkey-signature:received:received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=Kbxe/etMyR6rRrAA58T+O1lgnbQSCOZ5UDMznO/pX+yrQNfh3yZApkIEAPCD5PLlScm+zRjLBogD2QsjKEyzy3j+w5k0Lg2mH14rp6Q/9z4xJbqLI0ewLf7hcxZk9Sgs2qc5AoSRpvEab7SBv6w+F0J22GpokoOXuWk+hyJSixc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=beta; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=WdV3pdygQOt/XgeDz01V52l05rEHb5jVh9rByPr7HNIupXMbGhRiAhOadEord0O4zLM/BMLgR5GA3H0doqsHOqSPUHk/eyGvmuqRp/Dk4o88cOLCHBsWDhRGiycb0ZGS6TulmoavYYyTp2CR3Fy3MdEwbNNTAhaFARgCvPkS/NA= Received: by 10.78.201.2 with SMTP id y2mr4547941huf.1175117498804; Wed, 28 Mar 2007 14:31:38 -0700 (PDT) Received: by 10.78.160.1 with HTTP; Wed, 28 Mar 2007 14:31:38 -0700 (PDT) Message-ID: <6b8c0db0703281431m4b14237es333e1d6152f124de@mail.gmail.com> Date: Wed, 28 Mar 2007 17:31:38 -0400 From: "=?ISO-8859-1?Q?Valent=EDn_Orfila?=" To: "Mihail Mihailov" Subject: Re: Retrieving result of COUNT(*) with PHP Cc: pgsql-php@postgresql.org In-Reply-To: <20070329000511.qidyijwozuo0w0w4@imp3.uta.fi> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_20910_12533873.1175117498705" References: <20070329000511.qidyijwozuo0w0w4@imp3.uta.fi> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.429 tagged_above=0 required=5 tests=AWL=0.054, BAYES_50=0.001, HTML_30_40=0.374, HTML_MESSAGE=0.001, SPF_PASS=-0.001 X-Spam-Level: X-Archive-Number: 200703/58 X-Sequence-Number: 3770 ------=_Part_20910_12533873.1175117498705 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Where people this the way I do, count (*) could be slower I thing that's enough :) 2007/3/28, Mihail Mihailov : > > Hi, > > actually, I don't think you need pg_fetch_all. > I use pg_fetch_row for the purpose. What you get with the SELECT > clause is one row. > > E.g. like this: > $res =3D pg_fetch_row(pg_query("SELECT COUNT(*) AS result_count FROM > etc. WHERE etc.")); //Not a very good style :-), one should check if > the query runs > $count =3D $res[0]; > > Another way to calculate number of rows in the result is to use > pg_num_rows function. > $res =3D pg_query("SELECT COUNT(*) AS result_count FROM etc. WHERE etc.")= ; > $count =3D pg_num_rows($res); > > Enjoy! > > Mihail > > > > Quoting Lynna Landstreet : > > > Hi there, > > > > I'm trying to use a SELECT COUNT(*) to count how many results would be > > retrieved from a particular query (as part of the process of paginating > > search results). > > > > But I'm having trouble figuring out how to retrieve the result of the > count > > in PHP. The result on its own is a resource rather than a specific > value, > > but when I try to retrieve the result via pg_fetch_result, some kind of > > weird math error happens and I get a huge number that bears no > resemblance > > to the number of results the query actually gets when it runs (1,714,60= 8 > for > > a query that in actuality produces three results). > > > > I tried using an AS clause (SELECT COUNT(*) AS result_count FROM etc. > WHERE > > etc.) to give the result a name, but that didn't help, and when I tried > > using pg_fetch_all on the result to see exactly what it was retrieving, > I > > got this: > > > > Array > > ( > > [0] =3D> Array > > ( > > [result_count] =3D> 1714608 > > ) > > > > ) > > > > Again with the weird number. And yet, if I run the exact same query in > the > > SQL window of phpPgAdmin, I get the proper result count (3 in this > > instance). > > > > Does anyone know what's going on here? Can I just not use SELECT > COUNT(*) > > with PHP at all? > > > > I originally had the script running the actual query and then counting > the > > results, and then running it again with LIMIT and OFFSET to get one > page's > > worth of results, but it seemed wasteful to do it that way, so I was > trying > > to do it more efficiently... :-/ > > > > Thanks, > > > > Lynna > > > > -- > > Spider Silk Design - http://www.spidersilk.net > > 509 St Clair W Box 73576, Toronto ON Canada M6C 1C0 > > Tel 416.651.2899 - Toll Free 1.866.651.2899 - Cell 416.873.9289 > > > > ---------------------------(end of broadcast)--------------------------= - > > TIP 2: Don't 'kill -9' the postmaster > > > > > ---------------------------(end of broadcast)--------------------------- > TIP 9: In versions below 8.0, the planner will ignore your desire to > choose an index scan if your joining column's datatypes do not > match > --=20 *************************************************************************** Ministerio de Planificaci=F3n y Desarrollo de la Rep=FAblica Bolivariana de Venezuela Direcci=F3n de Planes de Personal Jos=E9 Valent=EDn Orfila Brice=F1o Programador II Tel=E9fonos: Celular: (0416) 4131418 E-mail: valentinorfila@gmail.com valentinorfila@hotmail.com josevalentinorfila@yahoo.com Messenger: valentinorfila@hotmail.com Skype: valentinorfila valentinorfila@gmail.com *************************************************************************** ------=_Part_20910_12533873.1175117498705 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline
Where people this the way I do, count (*) could be slower

<?=
$sql =3D "select * from TableName";
$count =3D pg_query($s= ql);   
$count =3D pg_num_rows($count);
?>
<= td ><?=3D$count?></td>

I thing that's enough :)

2007/3/28, Mihail Mihailov <= Mihail.Mihailov@uta.fi>:
Hi,

actually, I don't think you need pg_fetch_all.
I use pg_f= etch_row for the purpose. What you get with the SELECT
clause is one row= .

E.g. like this:
$res =3D pg_fetch_row(pg_query("SELECT COU= NT(*) AS result_count FROM
etc. WHERE etc.")); //Not a very good style :-), one should check = if
the query runs
$count =3D $res[0];

Another way to calculate= number of rows in the result is to use
pg_num_rows function.
$res = =3D pg_query("SELECT COUNT(*) AS result_count FROM etc. WHERE etc.&quo= t;);
$count =3D pg_num_rows($res);

Enjoy!

Mihail


Quoting Lynna Landstreet <lynn= a@spidersilk.net>:

> Hi there,
>
> I'm try= ing to use a SELECT COUNT(*) to count how many results would be
> retrieved from a particular query (as part of the process of pagin= ating
> search results).
>
> But I'm having trouble f= iguring out how to retrieve the result of the count
> in PHP. The res= ult on its own is a resource rather than a specific value,
> but when I try to retrieve the result via pg_fetch_result, some ki= nd of
> weird math error happens and I get a huge number that bears n= o resemblance
> to the number of results the query actually gets when= it runs (1,714,608 for
> a query that in actuality produces three results).
>
>= I tried using an AS clause (SELECT COUNT(*) AS result_count FROM etc. WHER= E
> etc.) to give the result a name, but that didn't help, and wh= en I tried
> using pg_fetch_all on the result to see exactly what it was retrie= ving, I
> got this:
>
> Array
>   &n= bsp; (
>         [0] =3D> = Array
>          &n= bsp; (
>         &n= bsp;   [result_count] =3D> 1714608
>           )
&= gt;
>     )
>
> Again with the weird = number. And yet, if I run the exact same query in the
> SQL window of= phpPgAdmin, I get the proper result count (3 in this
> instance).
>
> Does anyone know what's going on here? Can I just not = use SELECT COUNT(*)
> with PHP at all?
>
> I originally h= ad the script running the actual query and then counting the
> result= s, and then running it again with LIMIT and OFFSET to get one page's
> worth of results, but it seemed wasteful to do it that way, so I w= as trying
> to do it more efficiently... :-/
>
> Thanks,<= br>>
> Lynna
>
> --
> Spider Silk Design - http://www.spidersilk.net
> 509 St Clair W Box 73576, Toronto ON = Canada M6C 1C0
> Tel 416.651.2899 - Toll Free 1.866.651.2899 - Cell 4= 16.873.9289
>
> ---------------------------(end of broadcast)--= -------------------------
> TIP 2: Don't 'kill -9' the postmaster
>

<= br>---------------------------(end of broadcast)---------------------------=
TIP 9: In versions below 8.0, the planner will ignore your desire to
       choose an index scan if your joini= ng column's datatypes do not
       ma= tch



--
****************= ***********************************************************
Ministerio de Planificaci=F3n y Desarrollo de la Rep=FAblica Bolivarian= a de Venezuela
Direcci=F3n de Planes de Personal

Jos=E9 Valent= =EDn Orfila Brice=F1o
Programador II

Tel=E9fonos: Celular: (0416= ) 4131418

E-mail: valentinorfila@gmai= l.com
           <= a href=3D"mailto:valentinorfila@hotmail.com">valentinorfila@hotmail.com=
           josevalentinorfila@yahoo.com

Messenger: valentinorfila@hotmail.com

Skype: valentin= orfila
           valentinorfila@gmail.com
*******************************************************************= ******** ------=_Part_20910_12533873.1175117498705--