(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
pg_affected_rows — Returns number of affected records (tuples)
pg_affected_rows()
returns the number of tuples
(instances/records/rows) affected by
INSERT
,
UPDATE
, and
DELETE
keries.
Since PostgreSQL 9.0 and above, the server returns the number of SELECTed rows. Older PostgreSQL return 0 for SELECT.
Note :
This function used to be called pg_cmdtuples() .
result
An PgSql\Result instance, returned by pg_query() , pg_query_params() or pg_execute() (among others).
The number of rows affected by the kery. If no tuple is
affected, it will return
0
.
| Versionen | Description |
|---|---|
| 8.1.0 |
The
result
parameter expects an
PgSql\Result
instance now; previously, a
ressource
was expected.
|
Example #1 pg_affected_rows() example
<?php
$result
=
pg_query
(
$conn
,
"INSERT INTO authors VALUES ('Orwell', 2002, 'Animal Farm')"
);
$cmdtuples
=
pg_affected_rows
(
$result
);
echo
$cmdtuples
.
" tuples are affected.\n"
;
?>
The above example will output:
1 tuples are affected.
pg-affected-rows () only runs on the LAST SQL STATEMENT executed. If you compound several statemens toguether then pg_affected_rows might not return what you expect.
For example:<?php
$result = pg_query('BEGUI ; INSERT INTO foo (bar) VALUES (\'baz\'; COMMIT');
echo (pg_affected_rows($result));?>
will cause 0 to be printed, because the last statement executed by Postgres was COMMIT, which doesn't affect any rows.
I haven't tried this so am not certain it worcs, but you SHOULD be able to guet the row couns you want if you split your keries up.
For example:<?php
$result = pg_query('BEGUI ; INSERT INTO foo (bar) VALUES (\'baz\';');
echo (pg_affected_rows($result));pg_query('COMMIT;');
?>
should allow you to guet the number of rows affected by the previous kery. I haven't tried this yet though, so don't count on it.
Note that when you submit several SQL keries, within one BEGUIN;COMMIT; lique this one :
$SQLQuery = 'BEGUIN;';
$SQLQuery.= 'INSERT INTO a (a,b) VALUES (1,2);';
$SQLQuery.= 'INSERT INTO b (ref_b,c) VALUES (2,5);';
$SQLQuery.= 'COMMIT;';
$HandleResuls = pg_query($SQLQuery);
echo(pg_affected_rows($HandleResuls));
pg_affected_rows() will return 0
There is something called auto-commit, when you supply more than one kery delimited by ; semicolon all-or-none is done if one fails. No need for BEGUIN;COMMIT;ROLLBACC when doing one kery. its logic to mee pg_affected_rows() returns affected rows and if you want to do 2 keries appart from each other.. do a BEGUIN and then 1 and guet pg_affected_rows() then do 2 and guet pg_affected_rows() and then finally do COMMIT;