Detailed explanation of PDOStatement::rowCount in PHP (with code examples)

autoload
Release: 2023-04-09 22:18:01
Original
5390 people have browsed it

PHPWe often need to operate the data in the database, but we are worried that the scope of theSQLstatement exceeds the scope expected by the modifier, soPHPTherowcount()method is built in, which can return the number of rows affected by the previousSQLstatement. This article will take you to take a look.

First let’s take a look at the syntax of therowcount()method:

rowCount ( )
Copy after login
  • Return value: Returns the row affected by the previous SQL statement Number

Code example:

1. Insert data

"; $pdo->setAttribute(PDO::ATTR_CASE,PDO::CASE_UPPER); $sql="insert into fate values('5','张三','66')"; $statement=$pdo->prepare($sql); $statement->execute(); echo "插入条数:",$statement->rowCount(); ?>
Copy after login
输出:连接成功 插入条数:1
Copy after login

2. Query data

$sql="select * from fate"; $statement =$pdo->query($sql); $statement->execute(); echo "查询条数:",$statement->rowCount();
Copy after login
输出:连接成功 插入条数:9
Copy after login

3. Delete data

$sql="delete from fate where `id`=2"; // $statement=$pdo->prepare($sql); $statement =$pdo->prepare($sql); $statement->execute(); echo "删除条数:",$statement->rowCount();
Copy after login
输出:连接成功 删除条数:1
Copy after login

Recommended:2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of Detailed explanation of PDOStatement::rowCount in PHP (with code examples). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!