Just to clarify about the possible return values in this Manual those not familiar with PHP and MySQL.
"-1 indicates that the query returned an error."
-1 will be returned if the query itself can not be issued to the server, possibly because of syntax error AND if the last query was not either an Insert or Update statement.
mysqli_affected_rows
mysqli->affected_rows
(PHP 5)
mysqli->affected_rows — Retorna o número de linhas afetadas pela operação MySQL anterior
Descrição
Estilo de procedimento:
Estilo orientado a objetos (propriedades):
mysqli_affected_rows() retorna o número de linhas afetadas pela ultima consulta INSERT, UPDATE, ou DELETE associada ao parâmetro link indicado. Se a ultima consulta foi invalida, esta função irá retornar -1.
Nota: Para comandos SELECT mysqli_affected_rows() funciona como mysqli_num_rows().
A função mysqli_affected_rows() funciona apenas com consultas que modificam a tabela. Para poder retornar o número de linhas numa consulta SELECT, use a função mysqli_num_rows().
Valores de Retorno
Um inteiro maior do que zero indica o número de linhas afetadas ou obtidas. Zero indica que nenhum registro foi atualizado por um UPDATE, não foram encontradas linhas em uma claúsula WHERE na consulta ou que a consulta ainda não foi executada. -1 indica que a consulta retornou com erro.
Nota: Se o número de linhas afetadas for maior do que o maior valor inteiro possível, o número de linhas afetadas será retornado como uma string.
Veja também
Examplo
Example#1 Estilo orientado a objeto
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
/* Insert rows */
$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %d\n", $mysqli->affected_rows);
$mysqli->query("ALTER TABLE Language ADD Status int default 0");
/* update rows */
$mysqli->query("UPDATE Language SET Status=1 WHERE Percentage > 50");
printf("Affected rows (UPDATE): %d\n", $mysqli->affected_rows);
/* delete rows */
$mysqli->query("DELETE FROM Language WHERE Percentage < 50");
printf("Affected rows (DELETE): %d\n", $mysqli->affected_rows);
/* select all rows */
$result = $mysqli->query("SELECT CountryCode FROM Language");
printf("Affected rows (SELECT): %d\n", $mysqli->affected_rows);
$result->close();
/* Delete table Language */
$mysqli->query("DROP TABLE Language");
/* close connection */
$mysqli->close();
?>
Example#2 Estilo de procedimento
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
if (!$link) {
printf("Can't connect to localhost. Error: %s\n", mysqli_connect_error());
exit();
}
/* Insert rows */
mysqli_query($link, "CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %d\n", mysqli_affected_rows($link));
mysqli_query($link, "ALTER TABLE Language ADD Status int default 0");
/* update rows */
mysqli_query($link, "UPDATE Language SET Status=1 WHERE Percentage > 50");
printf("Affected rows (UPDATE): %d\n", mysqli_affected_rows($link));
/* delete rows */
mysqli_query($link, "DELETE FROM Language WHERE Percentage < 50");
printf("Affected rows (DELETE): %d\n", mysqli_affected_rows($link));
/* select all rows */
$result = mysqli_query($link, "SELECT CountryCode FROM Language");
printf("Affected rows (SELECT): %d\n", mysqli_affected_rows($link));
mysqli_free_result($result);
/* Delete table Language */
mysqli_query($link, "DROP TABLE Language");
/* close connection */
mysqli_close($link);
?>
Os exemplos acima irão produzir a seguinte saída:
Affected rows (INSERT): 984 Affected rows (UPDATE): 168 Affected rows (DELETE): 815 Affected rows (SELECT): 169
mysqli_affected_rows
31-Dec-2006 02:35
12-Apr-2006 05:48
mysqli_affected_rows will return 0 if you run a UPDATE command with the same variables that already exist
Here is a example
db row from 'test' table
----------
id = 1
name = test
mysqli_query($db,"UPDATE `test` SET `name`='test' WHERE `id`='1'");
$i=mysqli_affected_rows($db);
$i Will Be Zero Or False

mysqli