mysqli::$affected_rows
mysqli::$affected_rows
mysqli_affected_rows
(PHP 5, PHP 7)
mysqli :: $ affected_rows - mysqli_affected_rows - 获取之前MySQL操作中受影响的行数
Description
面向对象的风格
int $mysqli->affected_rows;
程序风格
int mysqli_affected_rows ( mysqli $link )
返回受上一个INSERT
,UPDATE
,REPLACE
或DELETE
查询影响的行数。
对于SELECT语句,mysqli_affected_rows()的
工作方式与mysqli_num_rows()类似。
Parameters
`link`
仅过程样式:由mysqli_connect()或mysqli_init()返回的链接标识符
Return Values
大于零的整数表示受影响或检索的行数。零表示没有记录更新UPDATE语句,没有行与查询中的WHERE
子句匹配,或者没有查询尚未执行。-1表示查询返回错误。
注意
:如果受影响的行数大于最大整数值(PHP_INT_MAX
),受影响的行数将作为字符串返回。
Examples
Example #1 $mysqli->affected_rows example
面向对象的风格
<?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(
?>
程序风格
<?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
?>
上面的例子会输出:
Affected rows (INSERT): 984
Affected rows (UPDATE): 168
Affected rows (DELETE): 815
Affected rows (SELECT): 169
© 1997–2017 The PHP Documentation Group
根据知识共享署名许可证v3.0或更高版本授权。