note 90798 added to function.pg-num-rows
| From: | strata_ranger at hotmail dot com | Date: | Sun, 10 May 2009 17:06:10 +0000 |
| Subject: | note 90798 added to function.pg-num-rows | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-154415@lists.php.net to get a copy of this message | ||
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:
<?php
// Example query. Let's say that this updates five rows in the source table.
$res = pg_query("Update foo set bar = 'new data' where foo.bar = 'old data'
");
pg_num_rows($res); // 0
pg_affected_rows($res); // 5
pg_fetch_all($res); // FALSE
// Same query, with a RETURNING clause.
$res = pg_query("Update foo set bar = 'new data' where foo.bar = 'old data'
RETURNING foo.pkey");
pg_num_rows($res); // 5
pg_affected_rows($res); // 5
pg_fetch_all($res); // Multidimensional array corresponding to our affected rows & returned
columns
?>
----
Server IP: 69.147.83.197
Probable Submitter: 76.2.25.104
----
Manual Page -- http://www.php.net/manual/en/function.pg-num-rows.php
Edit -- https://master.php.net/note/edit/90798
Del: integrated -- https://master.php.net/note/delete/90798/integrated
Del: useless -- https://master.php.net/note/delete/90798/useless
Del: bad code -- https://master.php.net/note/delete/90798/bad+code
Del: spam -- https://master.php.net/note/delete/90798/spam
Del: non-english -- https://master.php.net/note/delete/90798/non-english
Del: in docs -- https://master.php.net/note/delete/90798/in+docs
Del: other reasons-- https://master.php.net/note/delete/90798
Reject -- https://master.php.net/note/reject/90798
Search -- https://master.php.net/manage/user-notes.php