search
Homephp教程php手册PHP中mysqli_affected_rows作用行数返回值分析

这篇文章主要介绍了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
}


看下例子:

普通模式,代码如下:

复制代码 代码如下:

$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();
?>

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment