mysqli_stmt_affected_rows()函數(shù)返回最后執(zhí)行的語(yǔ)句更改,刪除或插入的行總數(shù).
mysqli_stmt_affected_rows()函數(shù)返回最近執(zhí)行的語(yǔ)句影響(更改,刪除,插入)的行數(shù)。
只有在INSERT,UPDATE或DELETE語(yǔ)句之后調(diào)用此函數(shù),該函數(shù)才能正常工作。如果您需要了解受SELECT查詢影響的行數(shù),則需要使用 mysqli_stmt_num_rows() 函數(shù)。
mysqli_stmt_affected_rows($stmt)
序號(hào) | 參數(shù)及說(shuō)明 |
---|---|
1 | stmt(必需) 這是表示執(zhí)行SQL查詢的語(yǔ)句的對(duì)象。 |
PHP mysqli_stmt_affected_rows()函數(shù)返回一個(gè)整數(shù)值,該值指示受先前(INSERT,UPDATE,REPLACE或DELETE)操作影響的行數(shù)。
如果語(yǔ)句有錯(cuò)誤,則此函數(shù)返回-1。如果沒(méi)有受影響的行,則此函數(shù)返回0。
此函數(shù)最初是在PHP版本5中引入的,并且可以在所有更高版本中使用。
假設(shè)我們已經(jīng)在MySQL數(shù)據(jù)庫(kù)中創(chuàng)建了一個(gè)名為employee的表,其內(nèi)容如下:
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()函數(shù)的用法(面向過(guò)程風(fēng)格),執(zhí)行update更新數(shù)據(jù)后,返回受影響的行數(shù):
<?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; //執(zhí)行語(yǔ)句 mysqli_stmt_execute($stmt); print("已更新的記錄......\n"); //受影響的行 $count = mysqli_stmt_affected_rows($stmt); //結(jié)束語(yǔ)句 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); print("受影響的行 ".$count); ?>
輸出結(jié)果
已更新的記錄...... 受影響的行 3
在面向?qū)ο箫L(fēng)格中,此函數(shù)的語(yǔ)法為$con->affected_rows;。以下是面向?qū)ο箫L(fēng)格中此函數(shù)的示例,執(zhí)行delete刪除數(shù)據(jù)后,返回受影響的行數(shù):
<?php //建立連接 $con = new mysqli("localhost", "root", "password", "mydb"); $con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); print("創(chuàng)建表.....\n"); $con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)"); print("插入記錄.....\n"); $stmt = $con -> prepare( "DELETE FROM Test WHERE Name in(?, ?)"); $stmt -> bind_param("ss", $name1, $name2); $name1 = 'Raju'; $name2 = 'Rahman'; print("記錄已刪除.....\n"); //執(zhí)行語(yǔ)句 $stmt->execute(); //Affected rows $count = $stmt ->affected_rows; print("受影響的行數(shù) ".$count); //結(jié)束語(yǔ)句 $stmt->close(); //關(guān)閉連接 $con->close(); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... 記錄已刪除..... 受影響的行數(shù) 2
如果查詢不影響任何行,讓我們檢查它的返回值-
<?php $con = @mysqli_connect("localhost", "root", "password", "mydb"); mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)"); print("創(chuàng)建表.....\n"); mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)"); print("插入記錄.....\n"); $stmt = mysqli_prepare($con, "DELETE FROM test where Age<?"); mysqli_stmt_bind_param($stmt, "i", $num); $num = 8; //執(zhí)行語(yǔ)句 mysqli_stmt_execute($stmt); //受影響的行 $count = mysqli_stmt_affected_rows($stmt); print("受影響的行數(shù)(當(dāng)查詢不執(zhí)行任何操作時(shí)): ".$count); //結(jié)束語(yǔ)句 mysqli_stmt_close($stmt); //關(guān)閉連接 mysqli_close($con); ?>
輸出結(jié)果
創(chuàng)建表..... 插入記錄..... 受影響的行數(shù)(當(dāng)查詢不執(zhí)行任何操作時(shí)): 0