PHP Doku:: Gibt die Anzahl betroffener Datensätze (Tupel) zurück - function.pg-affected-rows.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzDatenbankerweiterungenAnbieterspezifische DatenbankerweiterungenPostgreSQLPostgreSQL-Funktionenpg_affected_rows

Ein Service von Reinhard Neidl - Webprogrammierung.

PostgreSQL-Funktionen

<<PostgreSQL-Funktionen

pg_cancel_query>>

pg_affected_rows

(PHP 4 >= 4.2.0, PHP 5)

pg_affected_rowsGibt die Anzahl betroffener Datensätze (Tupel) zurück

Beschreibung

int pg_affected_rows ( resource $result )

pg_affected_rows() gibt die Anzahl der Tupels (Instanzen/Datensätze/Zeilen) zurück, die von einer ausgeführten INSERT-, UPDATE- oder DELETE-Abfrage betroffen sind.

Hinweis:

Diese Funktion hieß vormals pg_cmdtuples().

Parameter-Liste

result

PostgreSQL Ergebniskennung, die (unter anderem) von den Funktionen pg_query(), pg_query_params() oder pg_execute() zurückgegeben wird.

Rückgabewerte

Die Anzahl der Zeilen, die von der Abfrage betroffen wurden. Wurde keine Zeile betroffen, wird 0 zurückgegeben.

Beispiele

Beispiel #1 pg_affected_rows()-Beispiel

<?php
$result 
pg_query($conn"INSERT INTO authors VALUES ('Orwell', 2002, 'Animal Farm')");

$cmdtuples pg_affected_rows($result);

echo 
$cmdtuples " Zeile(n) wurden betroffen.\n";
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

1 Zeile(n) wurden betroffen.

Siehe auch

  • pg_query() - Führt eine Abfrage aus
  • pg_query_params() - Sendet ein Kommando zum Server und wartet seine Ausführung ab. Getrennt vom SQL-Kommando können dabei Parameter übergeben werden.
  • pg_execute() - Fordert den Datenankserver auf, eine vorbereitete Anfrage mit den angegebenen Parametern auszuführen und wartet auf das Ergebnis
  • pg_num_rows() - Gibt die Anzahl der Zeilen in einem Abfrageergebnis zurück


5 BenutzerBeiträge:
- Beiträge aktualisieren...
Anonymous
20.12.2007 12:02
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;
Anonymous
14.11.2007 16:45
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.

5.08.2005 12: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.06.2005 15: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.06.2005 10: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



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)