PHP: Hypertext Preprocessor (original) (raw)
pg_num_rows
(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
pg_num_rows — Returns the number of rows in a result
Description
Note:
This function used to be called pg_numrows().
Return Values
The number of rows in the result. On error, -1
is returned.
Changelog
Version | Description |
---|---|
8.1.0 | The result parameter expects an PgSql\Result instance now; previously, a resource was expected. |
Examples
Example #1 pg_num_rows() example
`<?php result=pgquery(result = pg_query(result=pgquery(conn, "SELECT 1");$rows = pg_num_rows($result);
echo
$rows . " row(s) returned.\n";
?>`
The above example will output:
See Also
- pg_num_fields() - Returns the number of fields in a result
- pg_affected_rows() - Returns number of affected records (tuples)
Found A Problem?
strata_ranger at hotmail dot com ¶
15 years ago
`As mentioned, if you are performing an INSERT/UPDATE or DELETE query and want to know the # of rows affected, you should use pg_affected_rows() instead of pg_num_rows().
However, you can also exploit postgres's RETURNING clause in your query to auto-select columns from the affected rows. This has the advantage of being able to tell not only how many rows a query affects, but exactly which rows those were, especially if you return a primary-key column.
For example:
`
francisco at natserv dot com ¶
17 years ago
`Not sure why this documentation doesn't have the following note:
Note: Use pg_affected_rows() to get number of rows affected by INSERT, UPDATE and DELETE query.
Found on other resources. Adding here in case someone else is looking for the info.
`
16 years ago
About preceding note, you shouldn't use pg_num_rows() for this. You should have instead a look at pg_affected_rows().