When operating databases using PHP, PDO provides powerful capabilities to perform SQL queries. When performing INSERT operations, understanding the number of rows affected is very important for subsequent processing. The PDOStatement::rowCount() method can help us get the number of rows affected after SQL is executed. This article will introduce in detail how to use rowCount to obtain the affected number of rows after performing INSERT operations using PDO and process them based on the result.
First, make sure you are connected to the database and ready to perform the INSERT operation. Here is a simple INSERT operation:
<?php
// Database connection configuration
$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'root';
$password = '';
try {
// create PDO Example
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Ready to insert data SQL Statement
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);
// Bind parameters
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
// Sample data
$name = 'John Doe';
$email = '[email protected]';
// implement INSERT operate
$stmt->execute();
// Get the number of rows affected
$affectedRows = $stmt->rowCount();
// Number of rows affected by the output
echo "Inserted $affectedRows Line data\n";
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
In the above code, $stmt->execute() performs an INSERT operation, and the $stmt->rowCount() method is used to return the number of rows affected. In INSERT operations, the number of rows affected, that is, the number of records inserted. If everything is OK, the returned value should be 1 , indicating that a row of data was successfully inserted.
Next, we can perform different processing based on the value returned by rowCount() . Normally, we can use this return value to determine whether the data is successfully inserted. For example:
<?php
if ($affectedRows > 0) {
echo "Data insertion successfully!\n";
} else {
echo "Data insertion failed!\n";
}
?>
Combined with the example above, we can put the whole process together to ensure that it is processed accordingly based on the insertion result.
<?php
// Database connection configuration
$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'root';
$password = '';
try {
// create PDO Example
$pdo = new PDO($dsn, $username, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Ready to insert data SQL Statement
$sql = "INSERT INTO users (name, email) VALUES (:name, :email)";
$stmt = $pdo->prepare($sql);
// Bind parameters
$stmt->bindParam(':name', $name);
$stmt->bindParam(':email', $email);
// Sample data
$name = 'John Doe';
$email = '[email protected]';
// implement INSERT operate
$stmt->execute();
// Get the number of rows affected
$affectedRows = $stmt->rowCount();
// Different processing is performed according to the number of rows affected
if ($affectedRows > 0) {
echo "Data insertion successfully!\n";
} else {
echo "Data insertion failed!\n";
}
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
rowCount() is not applicable to all database operations. For example, some database management systems (such as MySQL) return a value that is always 1 during an INSERT operation, even if no data is inserted. This situation will depend on the specific implementation of the database.
For operations that do not affect the number of rows, rowCount() may return 0 , so in actual development, it is recommended to check the return value after SQL is executed to ensure accurate insertion of the data.
The PDOStatement::rowCount() method is very useful after performing an INSERT operation. It can help developers check whether data has been inserted successfully. By performing different processing according to the value of rowCount() , we can effectively perform error processing and data verification.