pg_prepare()/pg_execute() and pg_query_params()

pg_prepare()/pg_execute() and pg_query_params()

am 26.05.2009 20:30:22 von Brandon Metcalf

I'm fairly new to PHP but have a great deal of Perl experience for
what it's worth. I'm having a problem where if I try to use a
parameterized query it fails but pg_last_error() returns nothing. For
example,

$result = pg_query_params($pgconn, $update, array($foo, $bar));

if (! $result) {
fwrite($fp, "before");
fwrite($fp, pg_last_error());
fwrite($fp, "after");
}

The strings "before" and "after" are showing up in $fp, but nothing
from pg_last_error(). Similarly, if I use pg_prepare() and
pg_execute(), pg_prepare() fails but nothing is returned from
pg_last_error().

Using pg_query() works, but I need to parameterize the SQL.

The versions I'm using are

$ php --version
PHP 5.2.4-2ubuntu5.5 with Suhosin-Patch 0.9.6.2 (cli) (built: Feb 11 2009 20:09:52)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

and PostgreSQL 8.3.6.

What am I missing?

--
Brandon

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: pg_prepare()/pg_execute() and pg_query_params()

am 26.05.2009 21:02:25 von Brandon Metcalf

b == brandon@geronimoalloys.com writes:

b> I'm fairly new to PHP but have a great deal of Perl experience for
b> what it's worth. I'm having a problem where if I try to use a
b> parameterized query it fails but pg_last_error() returns nothing. For
b> example,

b> $result = pg_query_params($pgconn, $update, array($foo, $bar));

b> if (! $result) {
b> fwrite($fp, "before");
b> fwrite($fp, pg_last_error());
b> fwrite($fp, "after");
b> }


I should also mentioned with

log_statement = 'all'

in postgresql.conf, nothing is dumped to the logs. This tells me
pg_query_params() isn't passing anything to the postgres server.

--
Brandon

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: pg_prepare()/pg_execute() and pg_query_params()

am 26.05.2009 21:41:35 von danaketh

--------------020508060408050904080007
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Can you provide us with some of the queries you send to database? I'd
suggest you test it with some easy and simple one, like this on from PHP
documentation:

|$result = pg_query_params($dbconn, 'SELECT * FROM shops WHERE name = $1', array("Joe's Widgets"));
|

Brandon Metcalf napsal(a):
> I'm fairly new to PHP but have a great deal of Perl experience for
> what it's worth. I'm having a problem where if I try to use a
> parameterized query it fails but pg_last_error() returns nothing. For
> example,
>
> $result = pg_query_params($pgconn, $update, array($foo, $bar));
>
> if (! $result) {
> fwrite($fp, "before");
> fwrite($fp, pg_last_error());
> fwrite($fp, "after");
> }
>
> The strings "before" and "after" are showing up in $fp, but nothing
> from pg_last_error(). Similarly, if I use pg_prepare() and
> pg_execute(), pg_prepare() fails but nothing is returned from
> pg_last_error().
>
> Using pg_query() works, but I need to parameterize the SQL.
>
> The versions I'm using are
>
> $ php --version
> PHP 5.2.4-2ubuntu5.5 with Suhosin-Patch 0.9.6.2 (cli) (built: Feb 11 2009 20:09:52)
> Copyright (c) 1997-2007 The PHP Group
> Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
>
> and PostgreSQL 8.3.6.
>
> What am I missing?
>
>

--

S pozdravem

Daniel Tlach
Freelance webdeveloper

Email: mail@danaketh.com
ICQ: 160914875
MSN: danaketh@hotmail.com
Jabber: danaketh@jabbim.cz


--------------020508060408050904080007--

Re: pg_prepare()/pg_execute() and pg_query_params()

am 26.05.2009 22:38:19 von danaketh

--------------070008030207060501060804
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Then you should put some debugging around the connection code.

|$pgconn = pg_connect("host=localhost port=5432 dbname=test user=test
password=test")| or die("Problem with connection to PostgreSQL:
".pg_last_error());

So you can be sure that you are really connected. Also you can use
pg_result_error()...

$result = pg_query_params($pgconn, $update, array($foo, $bar));
echo pg_result_error($result);

or even pg_result_error_field().


Brandon Metcalf napsal(a):
> b == brandon@geronimoalloys.com writes:
>
> b> I'm fairly new to PHP but have a great deal of Perl experience for
> b> what it's worth. I'm having a problem where if I try to use a
> b> parameterized query it fails but pg_last_error() returns nothing. For
> b> example,
>
> b> $result = pg_query_params($pgconn, $update, array($foo, $bar));
>
> b> if (! $result) {
> b> fwrite($fp, "before");
> b> fwrite($fp, pg_last_error());
> b> fwrite($fp, "after");
> b> }
>
>
> I should also mentioned with
>
> log_statement = 'all'
>
> in postgresql.conf, nothing is dumped to the logs. This tells me
> pg_query_params() isn't passing anything to the postgres server.
>
>

--

S pozdravem

Daniel Tlach
Freelance webdeveloper

Email: mail@danaketh.com
ICQ: 160914875
MSN: danaketh@hotmail.com
Jabber: danaketh@jabbim.cz


--------------070008030207060501060804--

Re: pg_prepare()/pg_execute() and pg_query_params()

am 27.05.2009 16:30:18 von Brandon Metcalf

d == danaketh@gmail.com writes:

d> Then you should put some debugging around the connection code.

d> |$pgconn = pg_connect("host=localhost port=5432 dbname=test user=test
d> password=test")| or die("Problem with connection to PostgreSQL:
d> ".pg_last_error());

d> So you can be sure that you are really connected. Also you can use
d> pg_result_error()...

d> $result = pg_query_params($pgconn, $update, array($foo, $bar));
d> echo pg_result_error($result);

d> or even pg_result_error_field().


I did have error checking around the call to pg_connect(). My problem
was twofold. One, I had a typo in the connection string and two, I
wasn't properly passing the error message back to top level
application, so I wasn't seeing the error from pg_connect().

Stupid mistake.

--
Brandon

--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php