Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id 4D59B9FB5B7 for ; Wed, 28 Mar 2007 17:35:03 -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 79699-04 for ; Wed, 28 Mar 2007 17:34:59 -0300 (ADT) X-Greylist: delayed 01:18:56.029785 by SQLgrey-1.7.4 Received: from tomts46-srv.bellnexxia.net (ftp1.sympatico.ca [209.226.175.190]) by postgresql.org (Postfix) with ESMTP id 5AF8F9FB3FE for ; Wed, 28 Mar 2007 17:34:54 -0300 (ADT) Received: from toip34-bus.srvr.bell.ca ([67.69.240.35]) by tomts24-srv.bellnexxia.net (InterMail vM.5.01.06.13 201-253-122-130-113-20050324) with ESMTP id <20070328191557.QRGI1540.tomts24-srv.bellnexxia.net@toip34-bus.srvr.bell.ca> for ; Wed, 28 Mar 2007 15:15:57 -0400 Received: from toroon01-1177844104.sdsl.bell.ca (HELO [192.168.2.96]) ([70.52.121.136]) by toip34-bus.srvr.bell.ca with ESMTP; 28 Mar 2007 15:15:54 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAABpbCkZGNHmI/2dsb2JhbAAN User-Agent: Microsoft-Entourage/10.1.6.040913.0 Date: Wed, 28 Mar 2007 15:16:07 -0400 Subject: Retrieving result of COUNT(*) with PHP From: Lynna Landstreet To: Message-ID: Mime-version: 1.0 Content-type: text/plain; charset="US-ASCII" Content-transfer-encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=BAYES_50=0.001, FORGED_RCVD_HELO=0.135 X-Spam-Level: X-Archive-Number: 200703/51 X-Sequence-Number: 3763 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,608 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] => Array ( [result_count] => 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