11.1. Connecting to the Database

The first thing you have to do when working with PHP and PostgreSQL is to connect to the database. Just as when you're working with a file, you need a handle to identify a connection and to interact with it. To generate this handle, you must use pg_connect. pg_connect establishesa connection to the database and returns a database handle. The connect string sent to the database is the same string you'd use in a C program. This is an important point because PHP's PostgreSQL interface is built on C, so the behavior of PHP is easy to understand if you are familiar with the C interface. However, to work with this book, you don't have to be familiar with C.

Let's see how to establish a connection to the database:

Get PHP and PostgreSQL: Advanced Web Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.