mysqli_stmt::$affected_rows

mysqli_stmt_affected_rows

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$affected_rows -- mysqli_stmt_affected_rowsDevuelve el número total de filas cambiadas, borradas, o insertadas por la última sentencia ejecutada

Descripción

Estilo orientado a objetos

Estilo por procedimientos

mysqli_stmt_affected_rows(mysqli_stmt $stmt): int

Devuelve el número de filas afectadas por una consulta INSERT, UPDATE, o DELETE.

Esta función sólo funciona con las consultas que actualizan una tabla. Con el fin de obtener el número de filas de una consulta SELECT, usar mysqli_stmt_num_rows() en su lugar.

Parámetros

stmt

Sólo estilo por procediminetos: Un identificador de declaraciones devuelto por mysqli_stmt_init().

Valores devueltos

Un entero mayor que cero indica el número de filas afectadas o recuperadas. Cero indica que no hubo registros actualizados para un sentencia UPDATE/DELETE, ninguna fila coincidio con la cláusula WHERE en la consulta o que ninguna consulta ha sido ejecutado. -1 Indica que la consulta ha devuelto un error. NULL indica un argumento no válido fue enviada a la función.

Nota:

Si el número de filas afectadas es mayor que el valor entero maximo de PHP, el número de filas afectadas se obtiene como una cadena.

Ejemplos

Ejemplo #1 Estilo orientado a objetos

<?php
$mysqli
= new mysqli("localhost", "mi_usuario", "mi_clave", "world");

/* verificar conexión */
if (mysqli_connect_errno()) {
printf("Error de conexión: %s\n", mysqli_connect_error());
exit();
}

/* crear tabla temporal */
$mysqli->query("CREATE TEMPORARY TABLE myCountry LIKE Country");

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

/* preparar sentencia */
if ($stmt = $mysqli->prepare($query)) {

/* Agrega variable para marcador de posición */
$code = 'A%';
$stmt->bind_param("s", $code);

/* ejecutar sentencia */
$stmt->execute();

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

/* cerrar sentencia */
$stmt->close();
}

/* cerrar conexión */
$mysqli->close();
?>

Ejemplo #2 Estilo por procedimientos

<?php
$link
= mysqli_connect("localhost", "mi_usuario", "my_clave", "world");

/* verificar conexión */
if (mysqli_connect_errno()) {
printf("Error de conexión: %s\n", mysqli_connect_error());
exit();
}

/* crear tabla temporal*/
mysqli_query($link, "CREATE TEMPORARY TABLE myCountry LIKE Country");

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

/* preparar sentencia */
if ($stmt = mysqli_prepare($link, $query)) {

/* Agrega variable para marcador de posición */
$code = 'A%';
mysqli_stmt_bind_param($stmt, "s", $code);

/* ejecutar sentencia */
mysqli_stmt_execute($stmt);

printf("filas insertadas: %d\n", mysqli_stmt_affected_rows($stmt));

/* cerrar sentencia */
mysqli_stmt_close($stmt);
}

/* cerrar conexión */
mysqli_close($link);
?>

El resultado del ejemplo sería:

filas insertadas: 17

Ver también

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