Re: [NOVICE] Re: psql with PHP question

From: Andrew McMillan <andrew(at)catalyst(dot)net(dot)nz>
To: Philip Hallstrom <philip(at)adhesivemedia(dot)com>
Cc: Chris Ryan <chris(at)greatbridge(dot)com>, Jason <gee308(at)mediaone(dot)net>, pgsql-php(at)postgresql(dot)org, pgsql-novice(at)postgresql(dot)org, pgsql-general(at)postgresql(dot)org
Subject: Re: [NOVICE] Re: psql with PHP question
Date: 2001-05-04 08:07:44
Message-ID: 3AF26350.A9F7AAC0@catalyst.net.nz
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice pgsql-php

Philip Hallstrom wrote:
>
> Also... is there any reason you don't just do:
>
> SELECT MAX(id) FROM article
>
> rather than what you have below? It would get you the same thing wouldn't
> it and save a query...
>
> On Thu, 3 May 2001, Chris Ryan wrote:
>
> > Jason,
> >
> > Look into the pg_result() function. You would use it something like
> > this:
> >
> > $q = pg_Exec("SELECT id FROM article WHERE id=(SELECT MAX(id) FROM
> > article)");
> > $maxid = pg_result($q,0,0); # pg_result($result,$row,$column_num)
> > echo "The highest id is ". $maxid[0];
> >
> > Hope this helps.
> >
> > Chris Ryan
> > chris(at)greatbridge(dot)com
> >
> > 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)");

Or even more efficiently:

SELECT id FROM article ORDER BY id DESC LIMIT 1;

Since that will use an index on article.id if article is a big table, has been
vacuum analyzed, and has an index available.

Regards,
Andrew.
--
_____________________________________________________________________
Andrew McMillan, e-mail: Andrew(at)catalyst(dot)net(dot)nz
Catalyst IT Ltd, PO Box 10-225, Level 22, 105 The Terrace, Wellington
Me: +64(21)635-694, Fax: +64(4)499-5596, Office: +64(4)499-2267xtn709

In response to

Browse pgsql-general by date

  From Date Subject
Next Message newsreader 2001-05-04 08:08:00 Re: a primer on trigger?
Previous Message Michaël Fiey 2001-05-04 07:55:40 Case sensitive order by

Browse pgsql-novice by date

  From Date Subject
Next Message Albert REINER 2001-05-04 09:59:13 UPDATE and INDEX on expensive function with (IsCachable)
Previous Message Peter J. Schoenster 2001-05-04 00:50:28 Re: input from a file

Browse pgsql-php by date

  From Date Subject
Next Message Joel Burton 2001-05-04 20:15:21 Re: [PHP] psql with PHP question
Previous Message Gyozo Papp 2001-05-03 16:19:10 Re: [PHP] psql with PHP question