dbTalk Databases Forums  

[PHP] pg_last_error() doesn't return connection errors

mailing.database.pgsql-php mailing.database.pgsql-php


Discuss [PHP] pg_last_error() doesn't return connection errors in the mailing.database.pgsql-php forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Frank Naude
 
Posts: n/a

Default [PHP] pg_last_error() doesn't return connection errors - 08-26-2010 , 06:44 AM






Hi,

I can successfully connect to PostgreSQL (pgsql extension). However,
when I make a deliberate mistake, I'm unable to capture the connection
error with pg_last_error() as one would with the *_last_error()
functions of other database extensions.

Here is an example:

<?php
$c = pg_connect('host=127.0.0.1 user=postgres password=xxx
dbname=yyy') or var_dump( pg_last_error() );
?>

The following output (with warnings) is returned:

Warning: pg_connect() [function.pg-connect]: Unable to connect to
PostgreSQL server: FATAL: password authentication failed for user
"postgres" in ..
Warning: pg_last_error() [function.pg-last-error]: No PostgreSQL link
opened yet in ...
bool(false)

One would expect pg_last_error() to return "password authentication
failed" instead of FALSE!

Best regards.

Frank

--
Sent via pgsql-php mailing list (pgsql-php (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php

Reply With Quote
  #2  
Old   
Andrew McMillan
 
Posts: n/a

Default Re: [PHP] pg_last_error() doesn't return connection errors - 08-26-2010 , 05:44 PM






On Thu, 2010-08-26 at 13:44 +0200, Frank Naude wrote:
Quote:
Hi,

I can successfully connect to PostgreSQL (pgsql extension). However,
when I make a deliberate mistake, I'm unable to capture the connection
error with pg_last_error() as one would with the *_last_error()
functions of other database extensions.

Here is an example:

?php
$c = pg_connect('host=127.0.0.1 user=postgres password=xxx
dbname=yyy') or var_dump( pg_last_error() );
?

The following output (with warnings) is returned:

Warning: pg_connect() [function.pg-connect]: Unable to connect to
PostgreSQL server: FATAL: password authentication failed for user
"postgres" in ..
Warning: pg_last_error() [function.pg-last-error]: No PostgreSQL link
opened yet in ...
bool(false)

One would expect pg_last_error() to return "password authentication
failed" instead of FALSE!
Hi Frank,

Presumably pg_last_error() doesn't do anything useful until after the
database connection has been made...

That behaviour doesn't seem particularly unnatural to me, really, though
this seems an unlikely place to be posting a bug about it. If it's a
problem for you perhaps you should file a bug with the developer.

Alternatively, does pg_errormessage() have the same flaw, or does that
give you the error details that you're after? That's what I use in my
own code, although I note that in my own code I only log the fact of
connection failure, without logging the reason, and I use that for
getting the error when a query fails. Perhaps something I should
improve :-)

Cheers,
Andrew.

--
------------------------------------------------------------------------
andrew (AT) morphoss (DOT) com +64(272)DEBIAN
Flexibility is overrated. Constraints are liberating.
------------------------------------------------------------------------


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iQIcBAABAgAGBQJMdu5OAAoJEOr8/r+P646/WqUP/1StfwqkOx/FoKkrr1MVajqW
fi52xNX2QtKlew4ZiOIOOphyFkVIY6krOMIM6AyH6lVIU1gxBC 1lC2JnYHJdcAOq
xsNNZwwAG46+5Rq+YtMRT89YMd3nPlSMZDMGF1gc2V3MoGmvit 4skyzUuabYam6m
dx5CbmcIs9eUAoB2sBvCnS56f/NkEcTxyRuvGa3QuNyqi+hP0V5VXqEaykW6xyd2
wsaYvb/VT8D7kXSNOnIhs/JibwUDdCeIs0iekNdhDQNeYhTRg9p9m3XQBIF0ehgm
vVV50QeSwoLPDGZBZPilCiao7q6x578gqNwrSyI1zqTQUFeH0o /ajTE3J8vBBLR6
OlFAgGe9tqi2xJ5LDJ6H7X6MoXMNABcyqjUO0bJGKlbj+h73BQ OqFpDvR+7RcTKc
vTX3TnAu82LIn9HfEcTX6kaFdOQztj4Bu5z9mGXFoyI2/OCnkiHYUKlgrg8Sep0d
M0zLl6WKEs0gZivACN5uzlGEpXggADrphHB3jS+TwKSmQko7KW IcZ17p/+n1Luzx
5PTvRr6XtnM0yGo7OHpCXcS8ogWVgmK989b9/h2YlyruDL0uXFX2+HptgHYlEJQX
umGaRb+WIKxbI/KN5iGR1eBeYE7WeCSi3LZTjXrXbcQ7h2Xef/7yHFo5RNFxxhAD
qP0ZZvDeGCAUDaHEy6Tg
=Ri52
-----END PGP SIGNATURE-----

Reply With Quote
  #3  
Old   
Frank Naude
 
Posts: n/a

Default Re: [PHP] pg_last_error() doesn't return connection errors - 08-27-2010 , 04:17 AM



Hi Andrew,

Thank you very much for the reply!

Quote:
Alternatively, does pg_errormessage() have the same flaw...?
Unfortunately pg_errormessage() doesn't work either. It returns:

Warning: pg_errormessage() [function.pg-errormessage]: No PostgreSQL
link opened yet in...

Quote:
That behaviour doesn't seem particularly unnatural to me,...
It is inconsistent with how the other DB modules behave. I can provide
you with some examples if it would help.

Best regards.

Frank

--
Sent via pgsql-php mailing list (pgsql-php (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php

Reply With Quote
  #4  
Old   
Grzegorz Ornoch
 
Posts: n/a

Default Re: [PHP] pg_last_error() doesn't return connection errors - 08-27-2010 , 05:22 AM



Hi
Try this function

/**
* @name pg_error
*
* @param pg resource result
* @param pg resource connection
*
* @return string content of the error pg
*
*/
function pg_error($result, $connection)
{
return ($result === false ? pg_last_error($connection) :
pg_result_error($result));
}
--
Regards
Grzegorz Ornoch


On Fri, Aug 27, 2010 at 11:17 AM, Frank Naude <naudefj (AT) gmail (DOT) com> wrote:

Quote:
Hi Andrew,

Thank you very much for the reply!

Alternatively, does pg_errormessage() have the same flaw...?

Unfortunately pg_errormessage() doesn't work either. It returns:

Warning: pg_errormessage() [function.pg-errormessage]: No PostgreSQL
link opened yet in...

That behaviour doesn't seem particularly unnatural to me,...

It is inconsistent with how the other DB modules behave. I can provide
you with some examples if it would help.

Best regards.

Frank

--
Sent via pgsql-php mailing list (pgsql-php (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php

Reply With Quote
  #5  
Old   
Frank Naude
 
Posts: n/a

Default Re: [PHP] pg_last_error() doesn't return connection errors - 08-27-2010 , 07:26 AM



Hi Grzegorz,

On Fri, Aug 27, 2010 at 12:22 PM, Grzegorz Ornoch <yanegorz (AT) gmail (DOT) com> wrote:
Quote:
Try this function...
Nice function, but since we cannot connect, and we do not have a
connection, it wouldn't help much either.

Best regards.

Frank

--
Sent via pgsql-php mailing list (pgsql-php (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php

Reply With Quote
  #6  
Old   
Frank Naude
 
Posts: n/a

Default Re: [PHP] pg_last_error() doesn't return connection errors - 08-27-2010 , 07:33 AM



Hi Andrew,

On Fri, Aug 27, 2010 at 2:01 PM, Andrew McMillan <andrew (AT) morphoss (DOT) com> wrote:
Quote:
Since I've migrated to PDO, and only have a bit of legacy stuff around
that uses pg_* I don't think it will do me any good at all! *I really
try to let other people do all of the coding against other databases, so
I can luxuriate in the One True SQL :-)
I'm fully aware that the pdo_pgsql works as it should - here is an example:

try {
$c = new PDO('pgsql:host=127.0.0.1 user=postgres password=xxx
dbname=yyy', 'postgres', 'xxx');
} catch (PDOException $e) {
var_dump( $e->getMessage() );
}

However, the pgsql driver needs some updating or should be deprecated.

Best regards.

Frank

--
Sent via pgsql-php mailing list (pgsql-php (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-php

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.