Home >Backend Development >PHP Tutorial >Detailed explanation of PDOStatement::rowCount in PHP (with code examples)
PHP
We often need to operate the data in the database, but we are worried that the scope of the SQL
statement exceeds the scope expected by the modifier, so PHP
The rowcount()
method is built in, which can return the number of rows affected by the previous SQL
statement. This article will take you to take a look.
First let’s take a look at the syntax of the rowcount()
method:
rowCount ( )
Return value: Returns the row affected by the previous SQL statement Number
Code example:
1. Insert data
<?php $servername="localhost"; $username="root"; $password="root123456"; $dbname="my_database"; $pdo=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password); echo "连接成功"."<br>"; $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!