Can "classDBStatementextendsPDOStatement" return the original number of rows during a query?

Source: Internet
Author: User
Tags php database
Can "classDBStatementextendsPDOStatement" return the original number of rows during a query? I see this code everywhere, but nothing tells me how to use it? How to use it?
class Database extends PDO{    function __construct()    {        parent::__construct('mysql:dbname=test;host=localhost', 'root', '');        $this->setAttribute(PDO::ATTR_STATEMENT_CLASS, array('DBStatement', array($this)));    }}class DBStatement extends PDOStatement{    public $dbh;    protected function __construct($dbh)    {        $this->dbh = $dbh;        $this->setFetchMode(PDO::FETCH_OBJ);    }        public function foundRows()    {        $rows = $this->dbh->prepare('SELECT found_rows() AS rows', array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => TRUE));        $rows->execute();        $rowsCount = $rows->fetch(PDO::FETCH_OBJ)->rows;        $rows->closeCursor();        return $rowsCount;    }}


In my understanding, after the query is completed, the number of rows in the original record set can be returned by calling foundRows.
This prevents the execution of two SQL statements on the page (select count; select *)
I don't know. Is that true?
If so, how can I call it?

Please kindly advise me !!! Zero !!!


Reply to discussion (solution)

Do not drill corners
SELECT SQL _CALC_FOUND_ROWS * FROM tbl_name LIMIT 10;
SELECT FOUND_ROWS ();
And
Select count (*) FROM tbl_name
SELECT SQL _CALC_FOUND_ROWS * FROM tbl_name LIMIT 10;

Two queries are also executed.
When a where clause is available, the former is more efficient than the latter.

Php database functions cannot execute multiple SQL commands at a time to prevent SQL attacks.
For example, ($ id is used to input parameters)
Select * from tbl_name where id = $ id
When $ id = '1; delete from tbl_name ';, the query string becomes
Select * from tbl_name where id = 1; delete from tbl_name
If this delete from tbl_name is executed, the consequences can be imagined.
Although
Select * from tbl_name where id = '$ ID'
To prevent this situation.
However, php does not assume that everyone will enclose the numeric data in quotation marks.


Is it recommended that you write a select count statement honestly? Right?

Do not drill corners
SELECT SQL _CALC_FOUND_ROWS * FROM tbl_name LIMIT 10;
SELECT FOUND_ROWS ();
And
Select count (*) FROM tbl_name
SELECT SQL _CALC_FOUND_ROWS * FROM tbl_name LIMIT 10;

Two queries are also executed.
In ......

I know more about SQL injection. I have been developing ASP. NET for many years.
Now the company wants to use PHP. it looks good to see SQL _CALC_FOUND_ROWS, and it wants to improve the performance.

Do not drill corners
SELECT SQL _CALC_FOUND_ROWS * FROM tbl_name LIMIT 10;
SELECT FOUND_ROWS ();
And
Select count (*) FROM tbl_name
SELECT SQL _CALC_FOUND_ROWS * FROM tbl_name LIMIT 10;

Two queries are also executed.
In ......

SQL _CALC_FOUND_ROWS is a parameter only available in mysql.
SQL _CALC_FOUND_ROWS informs MySQL to calculate the number of rows in the result set without considering any LIMIT clause. You can use SELECT FOUND_ROWS () to restore the number of rows.

SQL _CALC_FOUND_ROWS is a parameter only available in mysql.
SQL _CALC_FOUND_ROWS informs MySQL to calculate the number of rows in the result set without considering any LIMIT clause. You can use SELECT FOUND_ROWS () to restore the number of rows.

Yes, I want to use it because I don't consider limit.

Otherwise, the length of the array is not OK.

# Didn't all the first half of 1 be said? It is up to you to decide whether to use or not.

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.