Received: from barry.mail.mindspring.net (barry.mail.mindspring.net [207.69.200.25]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f43ECKA59414 for ; Thu, 3 May 2001 10:12:20 -0400 (EDT) (envelope-from gee308@mediaone.net) Received: from comm2.tcgfinancial.net (user-2ive29a.dialup.mindspring.com [165.247.9.42]) by barry.mail.mindspring.net (8.9.3/8.8.5) with ESMTP id KAA01272; Thu, 3 May 2001 10:12:13 -0400 (EDT) Received: from mediaone.net (IDENT:root@comm2 [127.0.0.1]) by comm2.tcgfinancial.net (8.9.3/8.9.3) with ESMTP id JAA20924; Thu, 3 May 2001 09:43:44 -0400 Message-ID: <3AF16090.69AB7A8@mediaone.net> Date: Thu, 03 May 2001 09:43:44 -0400 From: Jason X-Mailer: Mozilla 4.72 [en] (X11; U; Linux 2.2.14-5.0 i686) X-Accept-Language: en MIME-Version: 1.0 To: Tod McQuillin CC: pgsql-php@postgresql.org Subject: Re: psql with PHP question References: Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Archive-Number: 200105/13 X-Sequence-Number: 322 Thanks, I'll use that function. I also didn't realize the second select statement was unnecessary. Jason Tod McQuillin wrote: > On Thu, 3 May 2001, Jason wrote: > > > Hi, I want to try and optimize my calls to postgreSQL from PHP. If > > I only need one field from a select statement, what should I use? I am > > currently using: > > $q = pg_Exec("SELECT id FROM article WHERE id=(SELECT MAX(id) > > FROM article)"); > > $maxid = pg_fetch_array($q, 0); > > echo "The highest id is ". $maxid[0]; > > Your select statement is the same thing as simply > > SELECT max(id) FROM article > > There is no reason that I can see to use the subselect unless you're > interested in multiple matching rows or other columns (which it appears > that you are not). > > > What can I use besides an array to get a single value? In general, using > > a single variable always saves more memory than using an array? > > Don't worry about saving memory. All your variables are dynamically > allocated and cleaned up automatically when no longer needed. > > To fetch a single value from a select statement, I use a function like > this one: > > function pg_fetch ($conn, $sql) { > $q = pg_exec($conn, $sql); > if (pg_numrows($q) > 0) { > return pg_result($q, 0, 0); > } else { > return ""; > } > } > > You can declare the function on a separate file if you'll use it from > multiple php scripts and then use require_once("functions.php") from each > script that uses it. > > Calling it is as easy as: > > $maxid = pg_fetch($conn, "SELECT max(id) FROM article"); > > Hope this helps. > -- > Tod McQuillin