PHP 8.4.1 Released!

pg_affected_rows

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

pg_affected_rows返回受影响的记录数(元组)

说明

pg_affected_rows(PgSql\Result $result): int

pg_affected_rows() 返回受 INSERTUPDATEDELETE 查询影响的元组数(实例/记录/行)。

从 PostgreSQL 9.0 及更高版本开始,服务器返回 SELECT 的行数。较旧的 PostgreSQL 则返回 0。

注意:

此函数过去称为 pg_cmdtuples()

参数

result

PgSql\Result 实例,由 pg_query()pg_query_params() 或者 pg_execute()(等)返回。

返回值

受查询影响的行数。如果没有元组受到影响,它将返回 0

更新日志

版本 说明
8.1.0 现在 result 参数接受 PgSql\Result 实例,之前接受 resource

示例

示例 #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";
?>

以上示例会输出:

1 tuples are affected.

参见

  • pg_query() - 执行查询
  • pg_query_params() - Submits a command to the server and waits for the result, with the ability to pass parameters separately from the SQL command text
  • pg_execute() - Sends a request to execute a prepared statement with given parameters, and waits for the result
  • pg_num_rows() - 返回结果中行的数量

添加备注

用户贡献的备注 4 notes

up
9
Anonymous
17 years ago
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.
up
-1
Bruno Baguette
19 years ago
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
up
-2
Anonymous
16 years ago
There 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;
up
-3
Anonymous
19 years ago
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.
To Top