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() は、INSERT, UPDATE, DELETE クエリにより変更されたタプル(インスタンス/レコード/行)の数を 返します。

PostgreSQL 9.0 以降は、SELECT を実行したときには選択された行の数を返すようになりました。 それ以前のバージョンでは、SELECT が返す結果は 0 でした。

注意:

この関数は、以前は pg_cmdtuples() と呼ばれていました。

パラメータ

result

pg_query()pg_query_params() や (様々な関数がありますが、特に) pg_execute() が返した PgSql\Result クラスのインスタンス。

戻り値

クエリによって変更された行の数を返します。もし変更されたタプルがない場合は 0 を返します。

変更履歴

バージョン 説明
8.1.0 result は、PgSql\Result クラスのインスタンスを期待するようになりました。 これより前のバージョンでは、リソース を期待していました。

例1 pg_affected_rows() の例

<?php
$result
= pg_query($conn, "INSERT INTO authors VALUES ('オーウェル', 2002, '動物農場')");

$cmdtuples = pg_affected_rows($result);

echo
$cmdtuples . " タプルが変更されました。\n";
?>

上の例の出力は以下となります。

1 タプルが変更されました。

参考

  • pg_query() - クエリを実行する
  • pg_query_params() - SQL コマンドとパラメータを分割してサーバーへ送信し、その結果を待つ
  • pg_execute() - 指定したパラメータを用いてプリペアドステートメントを実行するリクエストを 送信し、その結果を待つ
  • pg_num_rows() - 行数を返す

add a note add a note

User Contributed Notes 5 notes

up
7
Anonymous
16 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
18 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
0
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
0
Anonymous
18 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.
up
-4
Anonymous
18 years ago
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)
To Top