This property contains a -1 value in situations when no errors have occurred. When preparing a SELECT statement (or any other statement which doesn't affect rows), the property will be -1. The property will also be reset to -1 if an INSERT statement is re-prepared as a SELECT statement.
For example:
<?php
$mysqli = new mysqli('localhost', 'user', 'pass', 'test');
$stmt = $mysqli->stmt_init();
$param = 'value';
//First SELECT
$stmt->prepare('SELECT * FROM `test` WHERE `field`=?');
$stmt->bind_param('s', $param);
var_dump($stmt->execute());
var_dump($stmt->affected_rows);
echo '<br>';
//INSERT
$stmt->prepare('INSERT INTO `test` (`field`) VALUES (?)');
$stmt->bind_param('s', $param);
var_dump($stmt->execute());
var_dump($stmt->affected_rows);
echo '<br>';
//Second SELECT
$stmt->prepare('SELECT * FROM `test` WHERE `field`=?');
$stmt->bind_param('s', $param);
var_dump($stmt->execute());
var_dump($stmt->affected_rows);
echo '<br>';
?>
Displays:
bool(true) int(-1)
bool(true) int(1)
bool(true) int(-1)
mysqli_stmt->affected_rows
mysqli_stmt_affected_rows
(PHP 5)
mysqli_stmt->affected_rows -- mysqli_stmt_affected_rows — Devuelve el número total de filas cambiadas, borradas, o insertadas por la última sentencia ejecutada
Descripción
Estilo orientado a objetos
Estilo por procesos
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 procedimental: 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.
Note:
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
Example #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();
?>
Example #2 Estilo por procesos
<?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
- mysqli_stmt_num_rows() - Return the number of rows in statements result set
- mysqli_prepare() - Prepare an SQL statement for execution
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.
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.
