How does PDO calculate the number of result sets?

Source: Internet
Author: User
Tags learn php rowcount
There is such a piece of code, how to take the number of result sets .... Please take a look at the great God ...
$stmt = $dbc->prepare (' Select COUNT (*) from Loginlog ');
$rows = $stmt->execute ();
Pagedivide ($rows, 10);
$result = $dbc->prepare (' select * from Loginlog ORDER BY logintime desc limit $sqlfirst, $shownu ');
$result->execute ();
Echo ' A total '. $rows. ' Log in ';
Echo ';
Echo ';
Echo ';
Echo '




















'; Echo ' '; Echo ' '; echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; while ($row = $result->fetch ()) {echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; } Echo '
user namepasswordlogin IPlogin timelogin statusnumber of attemptsonline status
'. $row [' name ']. ''. $row [' Password ']. ''. $row [' IP ']. ''. $row [' Logintime ']. ''. $row [' status ']. ''.' 1 '. ''.' Online '. '
';
Echo '';
Echo ';
Echo ';
Echo ';


Reply to discussion (solution)

$result = $stmt->fetch (pdo::fetch_num);
echo $result [0]; This is

$result = $stmt->fetch (pdo::fetch_num);
echo $result [0]; This is
Is that how it is written?
$stmt = $dbc->prepare (' select * from Loginlog ');
$rows = $stmt->execute ();
$rowsNum = $rows->fetch (pdo::fetch_num);

LZ should be $stmt= $dbc->prepare (' select * from Loginlog ');
$rows = $stmt->execute ();
$rowsNum = $rows->fetch (PDO::FETCH_ASSOC);
Print_r ($rowsNum->rowcount ());

LZ should be $stmt= $dbc->prepare (' select * from Loginlog ');
$rows = $stmt->query ();
$rowsNum = $rows->fetch (PDO::FETCH_ASSOC);
Print_r ($rowsNum->rowcount ());
There is a small problem above, is Print_r ($rows->rowcount ());


LZ should be $stmt= $dbc->prepare (' select * from Loginlog ');
$rows = $stmt->query ();
$rowsNum = $rows->fetch (PDO::FETCH_ASSOC);
Print_r ($rowsNum->rowcount ());
There is a small problem above, is Print_r ($rows->rowcount ());
Fatal Error:call to a member function fetch () on a non-object I
$stmt = $dbc->prepare (' select * from Loginlog ');
$rows = $stmt->execute ();
$rowsNum = $rows->fetch (PDO::FETCH_ASSOC);
$rowsN = $rows->rowcount ();

$stmt = $dbc->prepare (' select * from Loginlog ');
$rows = $stmt->execute ();
$rowsNum = $stmt->fetch (pdo::fetch_num);

$stmt = $dbc->prepare (' select * from Loginlog ');
$rows = $stmt->execute ();
$rowsNum = $stmt->fetch (pdo::fetch_num);
Echo an array ... Want to rowcount ();

$stmt = $dbc->prepare (' Select COUNT (*) from Loginlog ');
$stmt->execute ();
$rowsNum = $stmt->fetch (pdo::fetch_num); Returns an array
Print_r ($rowsNum);

or this:
$stmt = $dbc->prepare (' Select COUNT (*) from Loginlog ');
$stmt->execute ();
$rowsNum = $stmt->fetchcolumn ();
Echo $rowsNum;

int Pdostatement::rowcount (void)

Pdostatement::rowcount () returns the number of rows affected by the previous delete, INSERT, or UPDATE statement executed by the corresponding Pdostatement object.

If the previous SQL statement executed by the related pdostatement is a SELECT statement, some data may return the number of rows returned by this statement. However, this approach does not guarantee that all data is valid and should not be relied upon for portable applications.

$stmt = $dbc->prepare (' Select COUNT (*) from Loginlog ');
$stmt->execute ();
$rowsNum = $stmt->fetch (pdo::fetch_num); Returns an array
Print_r ($rowsNum);

or this:
$stmt = $dbc->prepare (' Select COUNT (*) from Loginlog ');
$stmt->execute ();
$rowsNum = $stmt->fetchcolumn ();
Echo $rowsNum;

$stmt = $dbc->prepare (' Select COUNT (*) from Loginlog ');
$rows = $stmt->execute ();
$rowsNum = $rows->fetchcolumn ();
Pagedivide ($rowsNum, 10);
$result = $dbc->prepare (' select * from Loginlog ORDER BY logintime desc limit $sqlfirst, $shownu ');
$result->execute ();
if ($stmt) {
/* $result =mysql_query (' select * from Loginlog ORDER by logintime Desc ');
$total =mysql_num_rows ($result);
Pagedivide ($total, 10);
$result =mysql_query ("SELECT * from Loginlog ORDER BY logintime desc limit $sqlfirst, $shownu"); */
Echo ' A total '. $rowsNum. ' Log in ';
Echo ';
Echo ';
Echo ';
Echo '























'; Echo ' '; Echo ' '; echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; while ($tota =mysql_fetch_assoc ($result)) {while ($row = $result->fetch ()) {echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; Echo ' '; } Echo '
user namepasswordlogin IPlogin timelogin statusnumber of attemptsonline status
'. $row [' name ']. ''. $row [' Password ']. ''. $row [' IP ']. ''. $row [' Logintime ']. ''. $row [' status ']. ''.' 1 '. ''.' Online '. '
';
Echo '';
Echo ';
Echo ';
Echo ';

The code as a whole is like this, but the page is always error
Fatal Error:call to a member function Fetchcolumn () on a non-object why is this?

$rows = $stmt->execute ();
$rowsNum = $stmt->fetchcolumn ();

$rows = $stmt->execute ();
$rowsNum = $stmt->fetchcolumn ();
$stmt = $dbc->prepare (' Select COUNT (*) from Loginlog ');
$rows = $stmt->execute ();
$rowsNum = $rows->fetchcolumn ();
That's what it says. Error ~

This post was last edited by xuzuning on 2013-06-26 15:38:19

$stmt->execute ();
$rowsNum = $stmt->fetchcolumn ();

$stmt->execute ();
$rowsNum = $stmt->fetchcolumn ();
Yes, well, that's right. Thank you, can you tell me why? I'll avoid making such a mistake later.

PDO::p Repare Returns a Pdostatement object, which is your $stmt
Pdostatement::execute returns a logical value that indicates whether the execution was successful or not.
If you write $rows = $stmt->execute ();
So $rows just a true
Of course there is no Fetchcolumn method, so the error.

One is to read the manual, not only to see the usage, but also to learn to look at the prototype statement
The second is to learn to read the wrong message

PDO::p Repare Returns a Pdostatement object, which is your $stmt
Pdostatement::execute returns a logical value that indicates whether the execution was successful or not.
If you write $rows = $stmt->execute ();
So $rows just a true
Of course there is no Fetchcolumn method, so the error.

One is to read the manual, not only to see the usage, but also to learn to look at the prototype statement
The second is to learn to read the wrong message

Well, I understand, thank you. I have been the project-driven way to learn PHP, encountered problems before going through the manual ...

  • 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.