How can the PHP extension for PostgreSQL (php_pgsql.dll) be effectively utilized for database access?

To effectively utilize the PHP extension for PostgreSQL (php_pgsql.dll) for database access, you need to establish a connection to the PostgreSQL database, execute queries, fetch results, and handle errors appropriately.

// Establish a connection to the PostgreSQL database
$conn = pg_connect("host=localhost dbname=mydatabase user=myuser password=mypassword");

if (!$conn) {
    echo "Failed to connect to PostgreSQL: " . pg_last_error();
    exit;
}

// Execute a query
$result = pg_query($conn, "SELECT * FROM mytable");

if (!$result) {
    echo "Query failed: " . pg_last_error();
    exit;
}

// Fetch results
while ($row = pg_fetch_assoc($result)) {
    echo "Column 1: " . $row['column1'] . " - Column 2: " . $row['column2'] . "<br>";
}

// Close the connection
pg_close($conn);