Home>Article>Backend Development> Detailed explanation of PDOStatement::rowCount in PHP (with code examples)

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

autoload
autoload Original
2021-04-26 11:47:14 5421browse

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 ( )
  • 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(); ?>
输出:连接成功 插入条数:1

2. Query data

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

3. Delete data

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

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!

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