(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)
pg_affected_rows — Devuelve el número de filas afectadas
pg_affected_rows() devuelve el número de filas
afectadas por las consultas de tipo INSERT,
UPDATE y DELETE.
El servidor devuelve el número de filas seleccionadas por SELECT.
Nota:
Anteriormente, esta función se llamaba pg_cmdtuples().
result
El número de filas afectadas por la consulta. Si no hay tuplas
afectadas, la función devolverá 0.
| Versión | Descripción |
|---|---|
| 8.1.0 |
El parámetro result ahora espera una instancia de PgSql\Result
; anteriormente, se esperaba un resource .
|
Ejemplo #1 Ejemplo con pg_affected_rows()
<?php
$result = pg_query($conn, "INSERT INTO editeur VALUES ('Auteur')");
$cmdtuples = pg_affected_rows($result);
echo $cmdtuples . " filas han sido afectadas.\n";
?>El ejemplo anterior mostrará:
1 filas han sido afectadas.
pg-affected-rows () only runs on the LAST SQL STATEMENT executed. If you compound several statements together then pg_affected_rows might not return what you expect.
For example:
<?php
$result = pg_query ('BEGIN; 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 works, but you SHOULD be able to get the row counts you want if you split your queries up.
For example:
<?php
$result = pg_query ('BEGIN; INSERT INTO foo (bar) VALUES (\'baz\';');
echo (pg_affected_rows ($result));
pg_query ('COMMIT;');
?>
should allow you to get the number of rows affected by the previous query. I haven't tried this yet though, so don't count on it.Note that when you submit several SQL queries, within one BEGIN;COMMIT; like this one :
$SQLQuery = 'BEGIN;';
$SQLQuery.= 'INSERT INTO a (a,b) VALUES (1,2);';
$SQLQuery.= 'INSERT INTO b (ref_b,c) VALUES (2,5);';
$SQLQuery.= 'COMMIT;';
$HandleResults = pg_query($SQLQuery);
echo(pg_affected_rows($HandleResults));
pg_affected_rows() will return 0There is something called auto-commit, when you supply more than one query delimited by ; semicolon all-or-none is done if one fails. No need for BEGIN;COMMIT;ROLLBACK when doing one query. its logic to mee pg_affected_rows() returns affected rows and if you want to do 2 queries apart from each other.. do a BEGIN and then 1 and get pg_affected_rows() then do 2 and get pg_affected_rows() and then finally do COMMIT;