PHPPDOselect statement result row calculation

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.