PHP

34. PHP PDO insert, update, delete 한 행 수 확인하기

drizzle0925 2021. 7. 29. 15:31
728x90

https://www.php.net/manual/ja/pdostatement.rowcount.php

 

 

PDOStatement :: rowCount
public PDOStatement::rowCount ( void ) : int

마지막 SQL 문의 영향을 받는 행 수를 반환합니다.

 

 

PDOStatement::rowCount() 
returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.
If the last SQL statement executed by the associated PDOStatement was a SELECT statement, 
some databases may return the number of rows returned by that statement.
However, this behaviour is not guaranteed for all databases and should not be relied on for portable applications.

 

PDOStatement :: rowCount ()는 해당 PDOStatement 오브젝트에 의해 실행된 마지막 DELETE, INSERT 또는 UPDATE 문의 영향을 받는 행 수를 리턴합니다. 연관된 PDOStatement에 의해 실행된 마지막 SQL 문 이 SELECT 문인 경우 일부 데이터베이스는 해당 명령문에 의해 리턴된 행 수를 리턴할 수 있습니다.  그러나 이 동작이 모든 데이터베이스에 대해 보장되는 것은 아니며 휴대용 응용 프로그램에 의존해서는 안됩니다.

 

 

1) INSERT

<?php
$insertStmt = $dbh->prepare("INSERT INTO fruit ('name') VALUE ('apple')");
$insertStmt->execute();

$count = $insertStmt->rowCount();
print("INSERT $count rows.\n");

 

 

2) UPDATE

<?php
$updateStmt = $dbh->prepare("UPDATE fruit SET NAME = 'fruit'");
$updateStmt->execute();

$count = $updateStmt->rowCount();
print("UPDATE $count rows.\n");

 

 

3) DELETE

<?php
$deleteStmt = $dbh->prepare("DELETE FROM fruit");
$deleteStmt->execute();

$count = $deleteStmt->rowCount();
print("DELETE $count rows.\n");
728x90