Re: php + postgresql

From: adam_pgsql <adam_pgsql(at)witneyweb(dot)org>
To: admin <mick(at)mjhall(dot)org>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: php + postgresql
Date: 2008-07-24 10:15:22
Message-ID: 26F90319-C7D0-4D5B-9547-DC4F2962CFBD@witneyweb.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Mick,

> 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?

Do you have an example? and what makes you say they are persisting?

> 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'.

are you sure pg_fetch_array($query) is returning any rows? (try echo
$row[0]; within the while loop)

> 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)?

I often do something along the lines of this:

if($stat = pg_exec($dbh, $sql))
{
if($rows = pg_numrows($stat))
{
for($i=0; $i < $rows; $i++)
{
$data = pg_fetch_array($stat, $i);

# do something with $data
}
}
else{echo "no rows returned";}
}
else{echo "query failed";}

> 4.
> Another weird one was that this statement always failed:
>
> $name = "file.php";
> SELECT fld_content FROM tbl_page WHERE fld_name='$name'

is $name being interpolated correctly when you use it.... maybe use:

$sql = "SELECT fld_content FROM tbl_page WHERE fld_name='".$name."'";

(or use a prepared statement)

> 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

Browse pgsql-general by date

  From Date Subject
Next Message Raymond O'Donnell 2008-07-24 10:16:34 Re: php + postgresql
Previous Message Richard Huxton 2008-07-24 10:13:41 Re: php + postgresql