"Class Dbstatement extends Pdostatement" is it possible to return the original number of rows at query time?

Source: Internet
Author: User
Tags php database
I see this code everywhere, but there's no place to tell what's the use? How to use?
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;    }}


My understanding is that when the query is complete, then call FoundRows to return the number of original record sets.
You can avoid the problem of 2 SQL to be executed when paging (select Count;select *)
I don't know if that's true.
If yes, how do I call it?

Please enlighten the prawns!!! Grateful!!!


Reply to discussion (solution)

Don't be obsessed.
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;

The same is done two times query
When there is a WHERE clause, the former is much more efficient than the latter

PHP database functions do not allow multiple SQL commands to be executed at one time to prevent SQL attacks
For example ($id incoming 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 the delete from Tbl_name is executed, the consequences can be imagined.
Although
SELECT * from Tbl_name where id= ' $id '
Can prevent this from happening.
But PHP does not assume that everyone will enclose numeric data in quotation marks.


Your suggestion is to honestly write a SELECT count statement? Right?

Don't be obsessed.
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;

The same is done two times query
In the ...

SQL injection I'm quite familiar with the development of ASP.
Now the company to use PHP, see sql_calc_found_rows looks good, want to improve performance

Don't be obsessed.
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;

The same is done two times query
In the ...

Sql_calc_found_rows is a parameter that only exists in MySQL
Sql_calc_found_rows tells MySQL how many rows should be in the result set, regardless of any limit clauses. The number of rows can be recovered using Select Found_rows ().

Sql_calc_found_rows is a parameter that only exists in MySQL
Sql_calc_found_rows tells MySQL how many rows should be in the result set, regardless of any limit clauses. The number of rows can be recovered using Select Found_rows ().

Yes, I just want to use it because I don't think about limit.

Otherwise directly take the length of the array is not OK

#1的前半段不是都说了吗? It's up to you to decide what to use or not to use.

  • Related Article

    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.