pg_affected_rows

(PHP 4 >= 4.2.0, PHP 5)

pg_affected_rows -- 返回受影响的记录数目

说明

int pg_affected_rows ( resource result )

pg_affected_rows() 返回在 pg_query() 中执行 INSERT,UPDATE 和 DELETE 查询后受到影响的记录数目(包括实例/记录/行)。如果本函数没有影响到任何记录,则返回 0。

例子 1. pg_affected_rows() 例子

<?php
$result
= pg_query($conn, "INSERT INTO authors VALUES ('Orwell', 2002, 'Animal Farm')");
$cmdtuples = pg_affected_rows($result);
echo
$cmdtuples . " tuples are affected.\n";
?>

注: 本函数以前被称为 pg_cmdtuples()

参见 pg_query()pg_num_rows()


add a note add a note User Contributed Notes
05-Aug-2005 06:31
That's not quite true, I've been able to execute multiple queries in a single call just fine. In stead, it has to do with the fact this function returns the affected rows for the last executed query, not the last set of queries specified to a single call to pg_query.
29-Jun-2005 09:15
Concering Bruno Baguette's note:

The pg_query function only allows one query per function call.  When you do your
$sql="BEGIN;
INSERT ...
COMMIT;";
$result=pg_query($conn,$sql);
echo pg_affected_rows($result);

you get a zero, because only the BEGIN; is executed.

The single query per call is, I beleive, a PHP builtin protection against SQL injection attacks.  (Ie someone submitting a string paramter that ends the current query and appends another one)
Bruno Baguette
28-Jun-2005 04:45
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 0