- PostgreSQL native functions.
- PDO (PHP Data Objects).
Connecting to PostgreSQL using native functions
PHP provides many functions for working directly with PostgreSQL databases. To connect to PostgreSQL using native functions, follow these steps:-
Use the following PHP code to connect to PostgreSQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
-
After the code connects to PostgreSQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable:
Connecting to PostgreSQL using PDO (PHP Data Objects)
The PostgreSQL functions in the previous procedure can only be used with PostgreSQL databases. PDO abstracts database access, and enables you to use code that can handle different types of databases. To connect to PostgreSQL using PDO, follow these steps:-
Use the following PHP code to connect to PostgreSQL and select a database. Replace username with your username, password with your password, and dbname with the database name:
-
After the code connects to PostgreSQL and selects the database, you can run SQL queries and perform other operations. For example, the following PHP code runs a SQL query that extracts the last names from the employees table, and stores the result in the $result variable:
More information
- For more information about PostgreSQL functions in PHP, please visit http://php.net/manual/en/book.pgsql.php.
- For more information about PDO, please visit http://www.php.net/manual/en/book.pdo.php.