定义和用法
这个mysqli_stmt_affected_rows()函数返回最近执行的语句影响(更改、删除、插入)的行数。
只有在 INSERT、UPDATE 或 DELETE 语句之后调用此函数才能正常工作。如果需要知道受 SELECT 查询影响的行数,则需要使用 mysqli_stmt_num_rows() 函数。
用法
mysqli_stmt_affected_rows($stmt)
参数
Sr.No | 参数及说明 |
---|---|
1 |
stmt(Mandatory) 这是一个对象,表示执行 SQL 查询的语句。 |
返回值
PHP mysqli_stmt_affected_rows() 函数返回一个整数值,表示受前一个(INSERT、UPDATE、REPLACE 或 DELETE)操作影响的行数。
如果语句有错误,则此函数返回-1.如果没有受影响的行,则此函数返回0。
PHP版本
这个函数最初是在 PHP 版本 5 中引入的,适用于所有后续版本。
示例
假设我们在 MySQL 数据库中创建了一个名为employee 的表,内容如下:
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() 函数的用法(程序风格) -
<?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;
//Executing the statement
mysqli_stmt_execute($stmt);
print("Records Updated......\n");
//Affected rows
$count = mysqli_stmt_affected_rows($stmt);
//Closing the statement
mysqli_stmt_close($stmt);
//Closing the connection
mysqli_close($con);
print("Rows affected ".$count);
?>
这将产生以下结果 -
Records Updated...... Rows affected 3
示例
在面向对象的风格中,这个函数的语法是 $con->affected_rows;以下是面向对象样式 $minus 中此函数的示例;
<?php
//Creating a connection
$con = new mysqli("localhost", "root", "password", "mydb");
$con -> query("CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
print("Table Created.....\n");
$con -> query("insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
print("Records Inserted.....\n");
$stmt = $con -> prepare( "DELETE FROM Test WHERE Name in(?, ?)");
$stmt -> bind_param("ss", $name1, $name2);
$name1 = 'Raju';
$name2 = 'Rahman';
print("Records Deleted.....\n");
//Executing the statement
$stmt->execute();
//Affected rows
$count = $stmt ->affected_rows;
print("Rows affected ".$count);
//Closing the statement
$stmt->close();
//Closing the connection
$con->close();
?>
这将产生以下结果 -
Table Created..... Records Inserted..... Records Deleted..... Rows affected 2
示例
如果查询不影响任何行,让我们检查它的返回值 -
<?php
$con = @mysqli_connect("localhost", "root", "password", "mydb");
mysqli_query($con, "CREATE TABLE Test(Name VARCHAR(255), AGE INT)");
print("Table Created.....\n");
mysqli_query($con, "insert into Test values('Raju', 25),('Rahman', 30),('Sarmista', 27)");
print("Records Inserted.....\n");
$stmt = mysqli_prepare($con, "DELETE FROM test where Age<?");
mysqli_stmt_bind_param($stmt, "i", $num);
$num = 8;
//Executing the statement
mysqli_stmt_execute($stmt);
//Affected rows
$count = mysqli_stmt_affected_rows($stmt);
print("Rows affected (when query does nothing):".$count);
//Closing the statement
mysqli_stmt_close($stmt);
//Closing the connection
mysqli_close($con);
?>
这将产生以下结果 -
Table Created..... Records Inserted..... Rows affected (when query does nothing):0
相关用法
- PHP mysqli_stmt_attr_get()用法及代码示例
- PHP mysqli_stmt_attr_set()用法及代码示例
- PHP mysqli_stmt_fetch()用法及代码示例
- PHP mysqli_stmt_data_seek()用法及代码示例
- PHP mysqli_stmt_free_result()用法及代码示例
- PHP mysqli_stmt_error()用法及代码示例
- PHP mysqli_stmt_field_count()用法及代码示例
- PHP mysqli_stmt_errno()用法及代码示例
- PHP mysqli_stmt_get_result()用法及代码示例
- PHP mysqli_stmt_init()用法及代码示例
- PHP mysqli_stmt_bind_result()用法及代码示例
- PHP mysqli_stmt_store_result()用法及代码示例
- PHP mysqli_stmt_reset()用法及代码示例
- PHP mysqli_stmt_param_count()用法及代码示例
- PHP mysqli_stmt_prepare()用法及代码示例
- PHP mysqli_stmt_sqlstate()用法及代码示例
- PHP mysqli_stmt_bind_param()用法及代码示例
- PHP mysqli_stmt_close()用法及代码示例
- PHP mysqli_stmt_num_rows()用法及代码示例
- PHP mysqli_stmt_result_metadata()用法及代码示例
注:本文由纯净天空筛选整理自 PHP mysqli_stmt_affected_rows() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。