downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

mysqli::autocommit> <MySQLi
Last updated: Fri, 13 Nov 2009

view this page in

mysqli->affected_rows

mysqli_affected_rows

(PHP 5)

mysqli->affected_rows -- mysqli_affected_rowsRetorna o número de linhas afetadas pela operação MySQL anterior

Descrição

Estilo orientado a objetos (propriedades):

mysqli
int $affected_rows;

Estilo de procedimento:

int mysqli_affected_rows ( mysqli $link )

mysqli_affected_rows() retorna o número de linhas afetadas pela ultima consulta INSERT, UPDATE, REPLACE ou DELETE associada ao parâmetro link indicado. Se a ultima consulta foi invalida, esta função irá retornar -1.

Para comandos SELECT mysqli_affected_rows() funciona como mysqli_num_rows().

Parâmetros

link

Apenas para estilo de procedimento: Um identificador de conexão retornado por mysqli_connect() or mysqli_init()

Valor Retornado

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.

Exemplos

Exemplo #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();
?>

Exemplo #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);
?>

O exemplo acima irá imprimir:

Affected rows (INSERT): 984
Affected rows (UPDATE): 168
Affected rows (DELETE): 815
Affected rows (SELECT): 169

Veja Também



add a note add a note User Contributed Notes
mysqli->affected_rows
oilpc at oilpc dot com
14-Nov-2009 04:06
For "INSERT" or "UPDATE" statement for modifying data contained in one row of one table I checked if number of affected rows equals 1 to determine success of the operation. It works fine both for errors and false value of WHERE condition (that might be generated according to specific application user acces privileges).
<?php
if ($mysqli->affected_rows==1){
    echo
"success";
}
else {
    echo
"fail";
}
?>

Checking if mysqli->affected_rows will equal -1 or not is not a good method of determining success of "INSERT IGNORE" statements. Example: Ignoring duplicate key errors while inserting some rows containing data provided by user only if they will match specified unique constraint causes returning of -1 value by mysqli->affected_rows even if rows were inserted. (checked on MySQL 5.0.85 linux and php 5.2.9-2 windows). However mysqli->sqlstate returns no error if statement was executed successfully.
<?php
if ($mysqli->affected_rows!=-1){
    echo
"success";// for "INSERT IGNORE" statements will not occur if there were any duplicate key errors ignored during execution of the query
}
else {
    echo
"fail";// "INSERT IGNORE" statements causing any duplicate key errors (however ignored) lead to mysqli->affected_rows equal -1
}

// Example below works for "INSERT IGNORE" stattements, too
if ($mysqli->sqlstate=="00000"){
    echo
"success";
}
else {
    echo
"fail";
}
?>

mysqli::autocommit> <MySQLi
Last updated: Fri, 13 Nov 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites