pg_affected_rows

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

pg_affected_rowsВозвращает количество затронутых запросом записей (кортежей)

Описание

int pg_affected_rows ( resource $result )

pg_affected_rows() возвращает количество кортежей (сущностей/записей/рядов) затронутых запросом INSERT, UPDATE, и DELETE queries.

С версии PostgreSQL 9.0 и новее, сервер возвращает число выбранных рядов для SELECT запроса. Более старые версии возвращают 0 для SELECT.

Замечание:

Ранее данная функция называлась pg_cmdtuples().

Список параметров

result

Результат запроса к PostgreSQL , значение типа resource, возвращаемое функцией pg_query(), pg_query_params() или pg_execute() (среди прочих).

Возвращаемые значения

Количество записей, затронутых запросом. Если ни один кортеж не был затронут, функция вернет 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 " кортежей затронуто.\n";
?>

Результат выполнения данного примера:

1 кортежей затронуто.

Смотрите также

  • pg_query() - Выполняет запрос
  • pg_query_params() - Посылает параметризованный запрос на сервер, параметры передаются отдельно от текста SQL запроса
  • pg_execute() - Запускает выполнение ранее подготовленного параметризованного запроса и ждет результат
  • pg_num_rows() - Возвращает количество строк в выборке

Коментарии

Автор:
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
2005-06-28 04:45:06
http://php5.kiev.ua/manual/ru/function.pg-affected-rows.html
Автор:
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)
2005-06-29 09:15:31
http://php5.kiev.ua/manual/ru/function.pg-affected-rows.html
Автор:
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.
2005-08-05 06:31:34
http://php5.kiev.ua/manual/ru/function.pg-affected-rows.html
Автор:
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.
2007-11-14 09:45:17
http://php5.kiev.ua/manual/ru/function.pg-affected-rows.html
Автор:
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;
2007-12-20 05:02:55
http://php5.kiev.ua/manual/ru/function.pg-affected-rows.html

    Поддержать сайт на родительском проекте КГБ