mysqli_stmt::$affected_rows

mysqli_stmt_affected_rows

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$affected_rows -- mysqli_stmt_affected_rowsRetorna o número total de linhas modificadas, apagadas, inseridas ou correspondidas pela última instrução executada

Descrição

Estilo orientado a objetos

Estilo procedural

mysqli_stmt_affected_rows(mysqli_stmt $statement): int|string

Retorna o número de linhas afetadas por consultas INSERT, UPDATE ou DELETE. Funciona como mysqli_stmt_num_rows() para instruções SELECT.

Parâmetros

statement

Somente no estilo procedural: Um objeto mysqli_stmt retornado por mysqli_stmt_init().

Valor Retornado

Um inteiro maior que zero indica o número de linhas afetadas ou recebidas. Zero indica que nenhum registro foi atualizado para uma instrução UPDATE, nenhuma linha correspondeu à cláusula WHERE na consulta ou que nenhuma consulta foi executada. -1 indica que a consulta retornou um erro ou que, para uma consulta SELECT, mysqli_stmt_affected_rows() foi chamada antes de mysqli_stmt_store_result().

Nota:

Se o número de linhas afetadas for maior que o máximo valor inteiro do PHP, o número de linhas afetadas será retornado como uma string.

Exemplos

Exemplo #1 Exemplo de mysqli_stmt_affected_rows()

Estilo orientado a objetos

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* cria tabela temporária */
$mysqli->query("CREATE TEMPORARY TABLE myCountry LIKE Country");

$query = "INSERT INTO myCountry SELECT * FROM Country WHERE Code LIKE ?";

/* prepara a instrução */
$stmt = $mysqli->prepare($query);

/* Vincula variável para o espaço reservado */
$code = 'A%';
$stmt->bind_param("s", $code);

/* executa a instrução */
$stmt->execute();

printf("Linhas inseridas: %d\n", $stmt->affected_rows);

Estilo procedural

<?php

mysqli_report
(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* cria tabela temporária */
mysqli_query($link, "CREATE TEMPORARY TABLE myCountry LIKE Country");

$query = "INSERT INTO myCountry SELECT * FROM Country WHERE Code LIKE ?";

/* prepara a instrução */
$stmt = mysqli_prepare($link, $query);

/* Vincula variável para o espaço reservado */
$code = 'A%';
mysqli_stmt_bind_param($stmt, "s", $code);

/* executa a instrução */
mysqli_stmt_execute($stmt);

printf("Linhas inseridas: %d\n", mysqli_stmt_affected_rows($stmt));

Os exemplos acima produzirão:

Linhas inseridas: 17

Veja Também

add a note

User Contributed Notes 2 notes

up
28
Carl Olsen
18 years ago
It appears that an UPDATE prepared statement which contains the same data as that already in the database returns 0 for affected_rows. I was expecting it to return 1, but it must be comparing the input values with the existing values and determining that no UPDATE has occurred.
up
-10
Chuck
16 years ago
I'm not sure whether or not this is the intended behavior, but I noticed through testing that if you were to use transactions and prepared statements together and you added a single record to a database using a prepared statement, but later rolled it back, mysqli_stmt_affected_rows will still return 1.
To Top