: This article describes how to calculate the number of rows returned by the PHPPDOselect statement. if you are interested in the PHP Tutorial, refer to it. 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 introduces the calculation of the number of rows in the result of the php pdo select statement, including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.