From: Tod McQuillin <devin@spamcop.net>
To: Jason <gee308@mediaone.net>
Cc: pgsql-php@postgresql.org
Subject: Re: psql with PHP question
Date: Thu, 3 May 2001 07:49:14 -0500 (CDT)
Message-ID: <Pine.GSO.4.31.0105030741430.9273-100000@sysadmin> (raw)
In-Reply-To: <3AF149C1.AEC30AA9@mediaone.net>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-php@postgresql.org
Cc: devin@spamcop.net, gee308@mediaone.net
Subject: Re: psql with PHP question
In-Reply-To: <Pine.GSO.4.31.0105030741430.9273-100000@sysadmin>
References: <3AF149C1.AEC30AA9@mediaone.net> <Pine.GSO.4.31.0105030741430.9273-100000@sysadmin>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox