Re: php + postgresql

From: "David Spadea" <david(dot)spadea(at)gmail(dot)com>
To: admin <mick(at)mjhall(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: php + postgresql
Date: 2008-07-24 15:13:52
Message-ID: 4ae0cb070807240813n329f9723m2e167533d68aeead@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Mick,

As I haven't seen anyone else say it, I just wanted to throw this in.

I'm not a PHP programmer, so I'm not very sure of PHP's scoping rules,
but this looks to me like a variable scoping problem. If the first
time you've used $content is inside of the while(), it's probably
going out of scope before your echo. Try this:

# Initialize $content before going into the loop.
# This declares it outside the scope of the while()

$content=''';

# Now do your loop

while ($row = pg_fetch_array($query)) {
$content = $row[0]
}

echo $content;

Your loop is a little weird, too. You're not accumulating anything,
you're just saving the previous value. When you exit the loop,
$content will only contain the value from the final row. If that's
your intent, you may save some time by reverse-ordering your query and
using "limit 1". That way you can remove the loop altogether and save
lots of processing time.

--
David Spadea

On Thu, Jul 24, 2008 at 5:41 AM, admin <mick(at)mjhall(dot)org> wrote:
> First, thanks to everyone who responded to my newbie questions yesterday,
> all clear now.
>
> I spent most of today struggling with apparently inconsistent behaviour
> while running SELECT statements on PG 8.1.9 using PHP 5.1.6 (these are both
> as supplied with CentOS 5.1, a fairly conservative distro).
>
> It seems that some of PHP's PG functions have changed recently, are there
> any known issues with them?
>
> 1.
> I ended up using pg_prepare() and pg_execute() as pg_query() alone just
> didn't seem to work. But SELECT statements seemed to be cached or persistent
> in some way, such that they "lived" beyond the life of the PHP script. Is
> there something I need to know about persistent behaviour in PG that doesn't
> exist in MySQL?
>
>
> 2.
> Another problem was that no matter how many times I checked and re-checked
> code, or which pg_fetch_* function I used, copying an array member and
> trying to use it later just would not work, eg
>
> while ($row = pg_fetch_array($query)) {
> $content = $row[0]
> }
>
> echo $content;
>
> $content was always 'undeclared'.
>
> 3.
> Some examples I found used PHP's pg_num_rows() function to count the rows in
> a result, then iterated through them with a "for" loop ... is this required
> behaviour (PHP docs don't appear to discuss this)?
>
> 4.
> Another weird one was that this statement always failed:
>
> $name = "file.php";
> SELECT fld_content FROM tbl_page WHERE fld_name='$name'
>
> while this one always worked:
>
> SELECT fld_content FROM tbl_page WHERE fld_pid=1
>
> in a three column table:
>
> fld_pid serial PRIMARY KEY,
> fld_name varchar(100) NOT NULL,
> fld_content text NOT NULL
>
> while everything worked fine from the psql console.
>
>
> ... but this post is getting too unwieldy. I am reading documentation but am
> also under some pressure to get basic things up and running. Any pointers to
> good documentation covering PHP + PG, or any well known gotchas?
>
> PS If people want to throw MySQL->PostgreSQL gotchas at me I'm happy to
> collate and write up.
>
> Thanks again
> Mick
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message John D. Burger 2008-07-24 15:21:23 FK check will use index on referring table?
Previous Message Kevin Neufeld 2008-07-24 15:13:04 Re: contrib catalogs