pg_send_prepare() asynchronously sends a prepared-statement definition to PostgreSQL. This guide explains the function with focused examples and the decisions that matter in real applications.
What Is pg_send_prepare() in PHP?
Asynchronously sends a prepared-statement definition to PostgreSQL.
A typical call looks like this:
<?php
pg_send_prepare($databaseConnection, "lookup_user", "SELECT name FROM users WHERE id = $1");
The input arguments shown in the call identify the relevant connection, resource, value, path or configuration for the operation. Pay attention to argument order and to the return value demonstrated below.
In the examples, $databaseConnection is a PostgreSQL connection created with pg_connect(). A $queryResult is produced by pg_query(). Large-object handles are transaction-scoped. A false result should be treated as a failed operation, not as empty data.
Example 1: Basic Usage
This first example uses pg_send_prepare() directly to demonstrate its basic purpose.
<?php
$connectionString = getenv("PG_CONNECTION_STRING");
if (!$connectionString) { throw new RuntimeException("Configure PG_CONNECTION_STRING first."); }
$databaseConnection = pg_connect($connectionString);
if ($databaseConnection === false) { throw new RuntimeException("Connection failed."); }
$result = pg_send_prepare($databaseConnection, "lookup_user", "SELECT name FROM users WHERE id = $1");
var_dump($result);
while (($pendingResult = pg_get_result($databaseConnection)) !== false) { echo pg_result_status($pendingResult, PGSQL_STATUS_STRING) . PHP_EOL; }
This call works with the particular connection or result object created above. The exact rows or database diagnostics depend on the example database.
Example 2: A Different Input or Option
The next example changes the input or option, showing another supported use of pg_send_prepare().
<?php
$connectionString = getenv("PG_CONNECTION_STRING");
if (!$connectionString) { throw new RuntimeException("Configure PG_CONNECTION_STRING first."); }
$databaseConnection = pg_connect($connectionString);
if ($databaseConnection === false) { throw new RuntimeException("Connection failed."); }
$result = pg_send_prepare($databaseConnection, "lookup_product", "SELECT name FROM products WHERE id = $1");
var_dump($result);
while (($pendingResult = pg_get_result($databaseConnection)) !== false) { echo pg_result_status($pendingResult, PGSQL_STATUS_STRING) . PHP_EOL; }
This call works with the particular connection or result object created above. The exact rows or database diagnostics depend on the example database.
Practical Example: Applying the Function
In application code, pg_send_prepare() belongs in a controlled workflow with input validation, a checked result and cleanup where needed.
<?php
$connectionString = getenv("PG_CONNECTION_STRING");
if (!$connectionString) { throw new RuntimeException("Configure PG_CONNECTION_STRING first."); }
$databaseConnection = pg_connect($connectionString);
if ($databaseConnection === false) { throw new RuntimeException("Connection failed."); }
$result = pg_send_prepare($databaseConnection, "lookup_user", "SELECT name FROM users WHERE id = $1");
var_dump($result);
while (($pendingResult = pg_get_result($databaseConnection)) !== false) { echo pg_result_status($pendingResult, PGSQL_STATUS_STRING) . PHP_EOL; }
This call works with the particular connection or result object created above. The exact rows or database diagnostics depend on the example database.
Important Behavior and Edge Cases
- Main behavior: Asynchronously sends a prepared-statement definition to PostgreSQL.
- Important limitation: Consume preparation results before sending dependent work.
- Requirements: PostgreSQL must be available, the pgsql extension must be enabled, and the connection must have suitable privileges.
Comparison With Related Functions
| Function or Approach | When to Use It |
|---|---|
pg_send_prepare() | Asynchronously sends a prepared-statement definition to PostgreSQL. |
pg_prepare | Creates a named prepared statement on a PostgreSQL connection for later execution. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the specific limits or preconditions of pg_send_prepare(). | Consume preparation results before sending dependent work. |
| Assuming every environment produces the same result. | PostgreSQL must be available, the pgsql extension must be enabled, and the connection must have suitable privileges. |
Key Takeaways
- Asynchronously sends a prepared-statement definition to PostgreSQL.
- Consume preparation results before sending dependent work.
- PostgreSQL must be available, the pgsql extension must be enabled, and the connection must have suitable privileges.
Use pg_send_prepare() when its behavior matches your task, and verify the relevant return value before taking further action. These examples are illustrative and require an existing PostgreSQL database containing the named example tables; do not run them against production data.