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
autoloadOriginal
2021-04-26 11:47:145516browse

    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 PHPThe 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(&#39;5&#39;,&#39;张三&#39;,&#39;66&#39;)";
$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