mysqli_stmt::$affected_rows
(PHP 5, PHP 7)
mysqli_stmt :: $ affected_rows - mysqli_stmt_affected_rows - 返回上一次执行语句更改,删除或插入的总行数
描述
面向对象的风格
int $mysqli_stmt->affected_rows;
程序风格
int mysqli_stmt_affected_rows ( mysqli_stmt $stmt )
返回受INSERT,UPDATE或DELETE查询影响的行数。
此函数仅适用于更新表的查询。为了从SELECT查询中获取行数,请改用mysqli_stmt_num_rows()。
参数
`stmt`
仅过程风格:由mysqli_stmt_init()返回的语句标识符。
Return Values
大于零的整数表示受影响或检索的行数。零表示没有记录更新UPDATE / DELETE语句,没有行与查询中的WHERE子句匹配,或者没有查询尚未执行。-1表示查询返回了一个错误。NULL表示提供给函数的参数无效。
注意:如果受影响的行数大于最大PHP int值,受影响的行数将作为字符串值返回。
例子
Example #1 Object oriented style
<?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();
}
/*?create?temp?table?*/
$mysqli->query("CREATE?TEMPORARY?TABLE?myCountry?LIKE?Country");
$query?=?"INSERT?INTO?myCountry?SELECT?*?FROM?Country?WHERE?Code?LIKE??";
/*?prepare?statement?*/
if?($stmt?=?$mysqli->prepare($query))?{
????/*?Bind?variable?for?placeholder?*/
????$code?=?'A%';
????$stmt->bind_param("s",?$code);
????/*?execute?statement?*/
????$stmt->execute();
????printf("rows?inserted:?%d\n",?$stmt->affected_rows);
????/*?close?statement?*/
????$stmt->close();
}
/*?close?connection?*/
$mysqli->close();
?>
Example #2 Procedural style
<?php
$link?=?mysqli_connect("localhost",?"my_user",?"my_password",?"world");
/*?check?connection?*/
if?(mysqli_connect_errno())?{
????printf("Connect?failed:?%s\n",?mysqli_connect_error());
????exit();
}
/*?create?temp?table?*/
mysqli_query($link,?"CREATE?TEMPORARY?TABLE?myCountry?LIKE?Country");
$query?=?"INSERT?INTO?myCountry?SELECT?*?FROM?Country?WHERE?Code?LIKE??";
/*?prepare?statement?*/
if?($stmt?=?mysqli_prepare($link,?$query))?{
????/*?Bind?variable?for?placeholder?*/
????$code?=?'A%';
????mysqli_stmt_bind_param($stmt,?"s",?$code);
????/*?execute?statement?*/
????mysqli_stmt_execute($stmt);
????printf("rows?inserted:?%d\n",?mysqli_stmt_affected_rows($stmt));
????/*?close?statement?*/
????mysqli_stmt_close($stmt);
}
/*?close?connection?*/
mysqli_close($link);
?>
上面的例子会输出:
rows inserted: 17
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com