PHP中mysqli_affected_rows作用行数返回值分析
网络编程 2021-07-05 09:50www.168986.cn编程入门
这篇文章主要介绍了PHP中mysqli_affected_rows作用行数返回值,实例分析了普通模式与oop模式的用法,具有一定的参考借鉴价值,需要的朋友可以参考下
本文实例分析了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
}
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();
}
/ 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 < 50″);
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);
$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 < 50″);
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模式,代码如下:
代码如下:
<?php
$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 < 50″);
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();
?>
$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 < 50″);
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();
?>
希望本文所述对大家的php程序设计有所帮助。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程