This article mainly introduces the use of the ROWCOUNT function of PDO and the efficiency of the problem, the need for friends can refer to the following
PDO has a function Pdostatement::rowcount returns the number of rows affected by the previous SQL statement.
The ROWCOUNT function is correct for a delete, INSERT, or UPDATE statement, but is related to the implementation of the database for the SELECT statement. Some databases read all of the result set into memory when executing a SELECT statement, but for a large number of result sets, this is obviously inefficient. Most databases only return part of the result set, and then return the rest of the result set when needed, so that both memory consumption and execution efficiency are optimized. In the latter case, rowcount cannot return the correct number of rows for the result set of the SELECT statement. There are several ways to get the correct number of rows for a select result
1. Use the Fetchall function $q = $db->query ("SELECT ..."); $rows = $q->fetchall (); $rowCount = count ($rows);
2. Use the SQL count function $q = $db->query ("SELECT count (*) from DB;"); $rows = $q->fetch (); $rowCount = $rows [0];
Obviously, the second method is more efficient.
Instance
Returns the number of rows deleted
Pdostatement::rowcount () returns the number of rows affected by the DELETE, INSERT, or UPDATE statement.
<?php/* Delete all rows from the FRUIT datasheet */$del = $DBH->prepare (' Delete from FRUIT '), $del->execute ();/* returns the number of rows deleted */ Print ("Return number of rows that were deleted:\n"), $count = $del->rowcount ();p rint ("deleted $count rows.\n");? >
The above instance output:
Return number of rows that were deleted:deleted 9 rows.