PHP中mysqli_affected_rows作用行数返回值分析

时间:2021-05-26

本文实例分析了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
}
看下例子:

普通模式,代码如下:
复制代码 代码如下:<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

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


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″);


mysqli_query($link, "UPDATE Language SET Status=1 WHERE Percentage > 50″);
printf("Affected rows (UPDATE): %dn", mysqli_affected_rows($link));


mysqli_query($link, "DELETE FROM Language WHERE Percentage < 50″);
printf("Affected rows (DELETE): %dn", mysqli_affected_rows($link));


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

mysqli_free_result($result);


mysqli_query($link, "DROP TABLE Language");


mysqli_close($link);
?>
oop模式,代码如下:
复制代码 代码如下:<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");


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


$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″);


$mysqli->query("UPDATE Language SET Status=1 WHERE Percentage > 50″);
printf("Affected rows (UPDATE): %dn", $mysqli->affected_rows);


$mysqli->query("DELETE FROM Language WHERE Percentage < 50″);
printf("Affected rows (DELETE): %dn", $mysqli->affected_rows);


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

$result->close();


$mysqli->query("DROP TABLE Language");


$mysqli->close();
?>

希望本文所述对大家的php程序设计有所帮助。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章