Php Postgres Error Message
Contents |
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers php pg_query error handling Security Introduction General considerations Installed as CGI binary Installed as pg_result_error_field an Apache module Session Security Filesystem Security Database Security Error Reporting Using Register Globals User Submitted pg_connect error 500 Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent pg error Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image
Postgres Last Error
Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box pg_last_notice » « pg_insert PHP Manual Function Reference Database Extensions Vendor Specific Database Extensions PostgreSQL PostgreSQL Functions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug pg_last_error (PHP 4 >= 4.2.0, PHP 5, PHP 7)pg_last_error — Get the last error message string of a connection Description string pg_last_error ([ resource $connection ] ) pg_last_error() returns the last er
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn
Pg_connect Or Die
more about Stack Overflow the company Business Learn more about hiring developers or pg_send_query posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community pg connect error Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up catching errors from Postgresql to PHP up vote 3 down http://php.net/manual/en/function.pg-last-error.php vote favorite 1 I want to catch and show the error(in a way that i choose) of a query on the web page using php . So instead of the code below $result=pg_query($connection,$query); if($result){ //success } else{ echo pg_last_error($connection); } can i use a method like error code matching or something else to achieve things like if(error equals duplicate value error){ echo "this value already exists"; } else http://stackoverflow.com/questions/12349230/catching-errors-from-postgresql-to-php if(error equals different type error){ echo "You should enter wrong type for column blabla" } Note I am using postgresql php sql postgresql error-handling share|improve this question edited Sep 10 '12 at 10:32 asked Sep 10 '12 at 9:52 woryzower 4741716 Yes, you can do this, but I'm not sure what you are asking. –Aleks G Sep 10 '12 at 9:57 @AleksG I don't want to use pg_last_error function . I want to write error message manually for corresponding error –woryzower Sep 10 '12 at 10:03 add a comment| 3 Answers 3 active oldest votes up vote 6 down vote accepted It's possible to retrieve the desirable standard SQLSTATE errcode, but there's a trick: the query has to be sent through the asynchronous pg_send_query() instead of the synchronous pg_query(). This is because pg_query() returns false on error instead of the resource that is required to peek at the error details. When pg_get_result() is called after pg_send_query, it will block anyway until the query is done, so it does not really complicate things compared to the synchronous case. And it returns a result which can be fully exploited for precise error handling. Example: if (pg_send_query($db, $query)) { $res=pg_get_result($db); if
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company http://stackoverflow.com/questions/3229388/catch-postgres-error-in-php Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs http://postgresql.nabble.com/pg-last-error-doesn-t-return-connection-errors-td2702160.html Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up catch postgres error in PHP up vote 0 down vote favorite 1 I have table in postgressql with primare key (pk_user). If connect error i insert wrong parametrs. postgres call exception:( ERROR: duplicate key value violates unique constraint "pk_user" ) this is ok, but i like catch this error and transform it to user interface ( this username is used) my php script: $sql="INSERT INTO user (....) VALUES (....)" @$result=pg_query($dbconn,$sql); if(!$result) { $error= pg_last_error($dbconn); if($error==='ERROR: duplicate key value violates unique constraint "pk_user"') $outputmesage="this username is used"; .... } else { ..... } but construction if($error==='ERROR: duplicate key value php postgres error violates unique constraint "pk_user"') is wrong. I don know what write this. Function strcmp(str1,str2) is wrong too. P.s: I'm sorry for my bad english php postgresql share|improve this question asked Jul 12 '10 at 14:27 Torenaga 1513 My comment is a bit irrelevant, but shouldn't you first check if user name is already used and then try to insert it? I do not like (maybe it's personal) the approach of doing something that I know it might be wrong and the see from the side-effects the cause. –dimitris mistriotis Jul 12 '10 at 15:51 1 dimitris mistriotis: No, this is the wrong way. Even if you check it first, you have no guarantee someone will not ask for the same username just between your check and insert (unless in a transaction - but that restricts concurrency and is needless there as just a single query suffices). Apart from the correctness of Torenaga's approach, it is also more effective, because it always needs just a single query, whereas you make two queries most of the time. –OndÅ™ej Bouda Jan 8 '13 at 16:55 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote You can use pg_result_error_field but then you have to use pg_send_query and pg_get_result (instead of pg_query and its better alternative p
Frank Naude Reply | Threaded Open this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ pg_last_error() doesn't return connection errors 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: 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 ([hidden email]) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-php Andrew McMillan Reply | Threaded Open this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ Re: pg_last_error() doesn't return connection errors On Thu, 2010-08-26 at 13:44 +0200, Frank Naude wrote: > 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: > > $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