When working with PHP, pg_affected_rows can help with a specific task: returns how many rows were affected by a PostgreSQL command result.
This guide shows what pg_affected_rows does, how to use it in code, and which details you should check before relying on the result.
What Is pg_affected_rows in PHP?
Returns how many rows were affected by a PostgreSQL command result.
Usage: The following examples show calls with suitable values. Consult the function parameters described in the examples, especially when optional arguments or extensions are involved.
Example 1: Basic Usage
The first example introduces pg_affected_rows with a direct PHP expression.
<?php
$databaseConnection = pg_connect(getenv("PG_CONNECTION_STRING")); if ($databaseConnection === false) { throw new RuntimeException("Connection failed"); } $queryResult = pg_query_params($databaseConnection, "UPDATE inventory SET quantity = quantity + 1 WHERE id = $1", [5]); if ($queryResult !== false) { echo pg_affected_rows($queryResult); }
The code is an illustration; its result depends on the relevant PHP extension, runtime state and input.
Example 2: A Different Input or Option
This second example demonstrates another input or condition, so you can compare the behavior with the first example.
<?php
$databaseConnection = pg_connect(getenv("PG_CONNECTION_STRING")); if ($databaseConnection === false) { throw new RuntimeException("Connection failed"); } $queryResult = pg_query_params($databaseConnection, "DELETE FROM expired_sessions WHERE id = $1", [25]); if ($queryResult !== false) { echo pg_affected_rows($queryResult); }
The code is an illustration; its result depends on the relevant PHP extension, runtime state and input.
Practical Example
Here is a small scenario illustrating how pg_affected_rows can fit into application code. Run the example only in an appropriate development environment if it reads files, changes data, starts processes or connects to a service.
<?php
$databaseConnection = pg_connect(getenv("PG_CONNECTION_STRING")); if ($databaseConnection === false) { throw new RuntimeException("Connection failed"); } $queryResult = pg_query_params($databaseConnection, "UPDATE users SET active = false WHERE id = $1", [7]); if ($queryResult === false) { throw new RuntimeException("Update failed"); } echo "Rows updated: " . pg_affected_rows($queryResult);
The code is an illustration; its result depends on the relevant PHP extension, runtime state and input.
Important Behavior and Edge Cases
- Core behavior: Returns how many rows were affected by a PostgreSQL command result.
- Important limitation: Requires a valid PgSql\Result; SELECT row count and affected row count are different concepts.
- Production use: Check return values and handle failures before acting on the result.
Comparison With Related Functions
| Function or Approach | Difference |
|---|---|
pg_affected_rows() | Returns how many rows were affected by a PostgreSQL command result. |
pg_num_rows() | Related approach. Its purpose and return value differ; select it when your task matches that operation. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Overlooking a special case of pg_affected_rows(). | Requires a valid PgSql\Result; SELECT row count and affected row count are different concepts. |
Using pg_affected_rows() without checking the outcome. | Handle failures and confirm that the output is suitable for the next step. |
Key Takeaways
- Returns how many rows were affected by a PostgreSQL command result.
- Requires a valid PgSql\Result; SELECT row count and affected row count are different concepts.
- Test the function with relevant inputs before using it in production.
Use pg_affected_rows when its behavior matches the job you need to complete. For dependable results, consider the input data, returned value and limitations shown above.