English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP MySQLi Manuale di riferimento
La funzione mysqli_stmt_affected_rows() restituisce il numero totale di righe modificate, eliminate o inserite dall'ultima query eseguita.
mysqli_stmt_affected_rows()La funzione restituisce il numero di righe influenzate dalla query eseguita recentemente (modifiche, eliminazioni, inserimenti).
Questa funzione funziona correttamente solo dopo un INSERT, UPDATE o DELETE statement. Se hai bisogno di sapere il numero di righe influenzate da una SELECT query, dovrai utilizzare mysqli_stmt_num_rows() Funzione.
mysqli_stmt_affected_rows($stmt)
Numero | Parametri e descrizione |
---|---|
1 | stmt(Obbligatorio) Questo è l'oggetto della query SQL eseguita. |
La funzione PHP mysqli_stmt_affected_rows() restituisce un valore intero che indica il numero di righe influenzate dal precedente operazione (INSERT, UPDATE, REPLACE o DELETE).
Se la query contiene errori, questa funzione restituisce-1.Se non ci sono righe interessate, questa funzione restituisce0.
Questa funzione è stata introdotta originariamente nella versione PHP 5 e può essere utilizzata in tutte le versioni successive.
Immaginiamo di aver già creato una tabella chiamata employee nel database MySQL, con il seguente contenuto:
mysql> select * from employee; +------------+--------------+------+------+--------+ | FIRST_NAME | LAST_NAME | AGE | SEX | INCOME | +------------+--------------+------+------+--------+ | Vinay | Bhattacharya | 20 | M | 21000 | | Sharukh | Sheik | 25 | M | 23300 | | Trupthi | Mishra | 24 | F | 51000 | | Sheldon | Cooper | 25 | M | 2256 | | Sarmista | Sharma | 28 | F | 15000 | +------------+--------------+------+------+--------+ 5 rows in set (0.00 sec)
以下示例演示了mysqli_stmt_affected_rows()函数的用法(面向过程风格),执行update更新数据后,返回受影响的行数:
<?php $con = mysqli_connect("localhost", "root", "password", "mydb"); $stmt = mysqli_prepare($con, "UPDATE employee set INCOME=INCOME-? where INCOME>=?"); mysqli_stmt_bind_param($stmt, "si", $reduct, $limit); $limit = 20000; $reduct = 5000; //Esegui dichiarazione mysqli_stmt_execute($stmt); print("已更新的记录......\n"); //Righe influenzate $count = mysqli_stmt_affected_rows($stmt); //Fine della dichiarazione mysqli_stmt_close($stmt); //Chiudi connessione mysqli_close($con); print("受影响的行 " . $count); ?>
Risultato output
已更新的记录...... 受影响的行 3
在面向对象风格中,此函数的语法为$con->affected_rows;。以下是面向对象风格中此函数的示例,执行delete删除数据后,返回受影响的行数:
<?php //建立连接 $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); print("Creazione tabella.....\n"); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)"); print("Inserimento record.....\n"); $stmt = $con -> prepare("DELETE FROM Test WHERE Name in(?, ?)"); $stmt->bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; print("Record cancellati.....\n"); //Esegui dichiarazione $stmt->execute(); //Righe influenzate $count = $stmt->affected_rows; print("Righe influenzate ". $count); //Fine della dichiarazione $stmt->close(); //Chiudi connessione $con->close(); ?>
Risultato output
Creazione tabella..... Inserimento record..... Record cancellati..... Righe influenzate 2
Se la query non influisce su nessuna riga, controlliamo il suo valore di ritorno-
<?php $con = @mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); print("Creazione tabella.....\n"); mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)"); print("Inserimento record.....\n"); $stmt = mysqli_prepare($con, "DELETE FROM test WHERE Age<?"); mysqli_stmt_bind_param($stmt, "i", $num); $num = 8; //Esegui dichiarazione mysqli_stmt_execute($stmt); //Righe influenzate $count = mysqli_stmt_affected_rows($stmt); print("Righe influenzate (quando la query non esegue alcuna operazione): ". $count); //Fine della dichiarazione mysqli_stmt_close($stmt); //Chiudi connessione mysqli_close($con); ?>
Risultato output
Creazione tabella..... Inserimento record..... Righe influenzate (quando la query non esegue alcuna operazione): 0