Can "classDBStatementextendsPDOStatement" return the original number of rows during a query? This post was last edited by rainxies from 2013-03-11:20:33. I can see this code everywhere, but nothing tells me how to use it? How to use it? Class & nbsp; "class DBStatement extends PDOStatement" can the original number of rows be returned during the query?
This post was edited by rainxies at 10:20:33. I can 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 !!!
------ 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.