Podręcznik PHP
Poprzedni |  | Następny

# mysqli_affected_rows

(PHP 5 CVS only)

mysqli_affected_rows

(no version information, might be only in CVS)

mysqli->affected_rows -- Gets the number of affected rows in a previous MySQL operation

## Description

Procedural style:

mixed mysqli_affected_rows ( object link)

Object oriented style (property):

class mysqli {
mixed affected_rows
}

mysqli_affected_rows() returns the number of rows affected by the last INSERT, UPDATE, or DELETE query associated with the provided `link` parameter. If the last query was invalid, this function will return -1.

> Notatka: For SELECT statements mysqli_affected_rows() works like mysqli_num_rows().

The mysqli_affected_rows() function only works with queries which modify a table. In order to return the number of rows from a SELECT query, use the mysqli_num_rows() function instead.

## Return Values

An integer greater than zero indicates the number of rows affected or retrieved. Zero indicates that no records where updated for an UPDATE statement, no rows matched the WHERE clause in the query or that no query has yet been executed. -1 indicates that the query returned an error.

> Notatka: If the number of affected rows is greater than maximal int value, the number of affected rows will be returned as a string.

## See also

mysqli_num_rows(), mysqli_info().

## Example

Przykład 1. Object oriented style
`<?php $mysqli = new mysqli("localhost", "my_user", "my_password", "test"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } /* create table and insert some data */ $mysqli->query("DROP TABLE IF EXISTS affected_rows"); $mysqli->query("CREATE TABLE affected_rows (a int)"); $mysqli->query("INSERT INTO affected_rows VALUES (1),(2),(3),(4)"); /* update values and retrieve number of affected rows */ $mysqli->query("UPDATE affected_rows SET a=5 WHERE a=1"); printf("Affected rows (update): %d\n", $mysqli->affected_rows); /* delete rows and retrieve number of affected_rows */ $mysqli->query("DELETE FROM affected_rows WHERE a < 4"); printf("Affected rows (delete): %d\n", $mysqli->affected_rows); /* select all rows and retrieve number of affected_rows */ $mysqli->query("SELECT a FROM affected_rows"); printf("Affected rows (select): %d\n", $mysqli->affected_rows); /* close connection */ $mysqli->close(); ?>`

Przykład 2. Procedural style
`<?php $link = mysqli_connect("localhost", "my_user", "my_password", "test") or die("Can't connect to MySQL server on localhost"); /* create table and insert some data */ mysqli_query($link, "DROP TABLE IF EXISTS affected_rows"); mysqli_query($link, "CREATE TABLE affected_rows (a int)"); mysqli_query($link, "INSERT INTO affected_rows VALUES (1),(2),(3),(4)"); /* update values and retrieve number of affected rows */ mysqli_query($link, "UPDATE affected_rows SET a=5 WHERE a=1"); printf("Affected rows (update): %d\n", mysqli_affected_rows($link)); /* delete rows and retrieve number of affected_rows */ mysqli_query($link, "DELETE FROM affected_rows WHERE a < 4"); printf("Affected rows (delete): %d\n", mysqli_affected_rows($link)); /* select all rows and retrieve number of affected_rows */ mysqli_query($link, "SELECT a FROM affected_rows"); printf("Affected rows (select): %d\n", mysqli_affected_rows($link)); /* close connection */ mysqli_close($link); ?>`

Poprzedni | Spis treści | Następny
Improved MySQL Extension | Początek rozdziału | mysqli_autocommit