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

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

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

    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    (   )
Copy after login
  • 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();
?>
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template