PHP connection to PostgreSQL

Source: Internet
Author: User
I am learning about PostgreSQL when I connect to PostgreSQL in PHP. I am using the PDO method. connection, addition, deletion, modification, and query can all be performed normally, but no error is reported for all wrong statements, for example, an incorrect SQL statement in mysql will be directly interrupted and an error message and error code will be output. However, postgresql only assigns a false value to the result without any information or prompts, I don't know if it is because of SQL syntax problems. In another case, when an erroneous SQL execution occurs in a transaction, the execution was successful... No catch error.
I don't know where the configuration is wrong or the operation method is incorrect. if you want to know more about it, click it. thank you!


Reply to discussion (solution)

Does PostgreSQL provide error numbers and error messages? This is obviously impossible, but you don't know how to check for errors.
PDO provides error messages in two ways:
1. return an array through the errorInfo method
Array (
0 => ansi SQL standard SQLSTATE error code
1 => error code defined by the database you use
2 => error information defined by the database you use
}
You can check whether the first item is correct or not.
This is the default PDO method.
2. set the property item PDO: ATTR_ERRMODE to PDO: ERRMODE_EXCEPTION to specify the exception throw mechanism.

Try {// perform various database operations here} catch (PDOException $ e) {die ("Error! : ". $ E-> getMessage ()." \ n ");}
In fact, the manual is very clear, but you have not carefully read it.

Does PostgreSQL provide error numbers and error messages? This is obviously impossible, but you don't know how to check for errors.
PDO provides error messages in two ways:
1. return an array through the errorInfo method
Array (
0 => ansi SQL standard SQLSTATE error code
1 => error code defined by the database you use
2 => error information defined by the database you use
}
You can check whether the first item is correct or not.
This is the default PDO method.
2. set the property item PDO: ATTR_ERRMODE to PDO: ERRMODE_EXCEPTION to specify the exception throw mechanism.

Try {// perform various database operations here} catch (PDOException $ e) {die ("Error! : ". $ E-> getMessage ()." \ n ");}
In fact, the manual is very clear, but you have not carefully read it.


I should read the manual carefully. thank you very much to the moderator.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.