This article mainly introduces the usage and efficiency of the rowCount function of PDO. For more information, see
This article mainly introduces the usage and efficiency of the rowCount function of PDO. For more information, see
The PDO function PDOStatement: rowCount returns the number of rows affected by the previous SQL statement.
The rowCount function has correct results for DELETE, INSERT, or UPDATE statements, but the select statement is related to the implementation of the database. Some databases read all the result sets into the memory when executing the select statement, but this is obviously inefficient for a large number of result sets. Most databases only return part of the result set. When necessary, they return the rest of the result set. This optimizes both memory usage and execution efficiency. In the latter case, rowCount cannot return the number of rows in the correct SELECT statement result set. There are several methods to obtain the number of rows in the correct 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.
The above is all the content of this article. I hope you will like it.