>백엔드 개발 >PHP 튜토리얼 >PHP中mysqli_affected_rows作用行数返回值_PHP教程

PHP中mysqli_affected_rows作用行数返回值_PHP教程

WBOY
WBOY원래의
2016-07-13 17:06:301358검색

本文章来给各位同学介绍关于PHP中mysqli_affected_rows作用行数返回值,有需要了解的朋友可参考。

mysqli中关于update操作影响的行数可以有两种返回形式:

1. 返回匹配的行数

2. 返回影响的行数

默认情况下mysqli_affected_rows返回的值为影响的行数,如果我们需要返回匹配的行数,可以使用mysqli_real_connect函数进行数据库连接的初始化,并在函数的flag参数位加上:


MYSQLI_CLIENT_FOUND_ROWS return number of matched rows, not the number of affected rows


普通格式:
int mysqli_affected_rows ( mysqli link )
oop格式:
class mysqli {
int affected_rows
}
看下例子:
普通模式

 代码如下 复制代码

$link = mysqli_connect("localhost", "my_user", "my_password", "world");

if (!$link) {
printf("Can't connect to localhost. Error: %sn", mysqli_connect_error());
exit();
}

/* Insert rows */
mysqli_query($link, "CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %dn", 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): %dn", mysqli_affected_rows($link));

/* delete rows */
mysqli_query($link, "DELETE FROM Language WHERE Percentage printf("Affected rows (DELETE): %dn", mysqli_affected_rows($link));

/* select all rows */
$result = mysqli_query($link, "SELECT CountryCode FROM Language");
printf("Affected rows (SELECT): %dn", mysqli_affected_rows($link));

mysqli_free_result($result);

/* Delete table Language */
mysqli_query($link, "DROP TABLE Language");

/* close connection */
mysqli_close($link);
?>


oop模式

 代码如下 复制代码

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %sn", mysqli_connect_error());
exit();
}

/* Insert rows */
$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %dn", $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): %dn", $mysqli->affected_rows);

/* delete rows */
$mysqli->query("DELETE FROM Language WHERE Percentage printf("Affected rows (DELETE): %dn", $mysqli->affected_rows);

/* select all rows */
$result = $mysqli->query("SELECT CountryCode FROM Language");
printf("Affected rows (SELECT): %dn", $mysqli->affected_rows);

$result->close();

/* Delete table Language */
$mysqli->query("DROP TABLE Language");

/* close connection */
$mysqli->close();
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/630691.htmlTechArticle本文章来给各位同学介绍关于PHP中mysqli_affected_rows作用行数返回值,有需要了解的朋友可参考。 mysqli中关于update操作影响的行数可以有两种返...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.