Introduction to the rowCount function of PDO

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

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.