欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-06-05 16:06:42
本文实例分析了php中mysqli_affected_rows作用行数返回值。分享给大家供大家参考。具体分析如下: mysqli中关于update操作影响的行数可以有两种...

本文实例分析了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();
}
 
/* 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();
?>

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