How does PDO move the result set pointer to the specified record location?

Source: Internet
Author: User
How does PDO move the result set pointer to the specified record location? mysql built-in function mysql_data_seek can move the pointer to the specified record location. does PDO provide similar functions, because I want to use this paging program, I checked a function of FETCH_ORI_ABS in PDO. it seems that it can also move pointers, but I don't know how to use it.


Reply to discussion (solution)

Http://www.sitepoint.com/forums/showthread.php? 709890-Equivalent-of-mysqli_data_seek-in-PDO
Depressed, this post says this is impossible.
Can't I implement the paging program below?
$ Dsn = "mysql: host = localhost; dbname = guestbook ";
$ Db = new PDO ($ dsn, 'root', '123'); // connect to the database
$ Db-> query ('set names gb2312 '); // sets the character set.
If (isset ($ _ GET ['Page']) & (int) $ _ GET ['Page']> 0) // Obtain the page number
$ Page = $ _ GET ['Page'];
Else
$ Page = 1;
// Set the number of records displayed on each page
$ PageSize = 4;
$ Keyword = trim ($ _ GET ['keyword']);

If ($ keyword <> "")
{
$ SQL = "select * from lyb where title like '%? % '";
$ Result = $ db_prepare ($ SQL); // create a result set
$ Result-> bindParam (1, $ keyword );

}
Else $ result = $ db-> prepare ("select * from lyb", array (PDO: ATTR_CURSOR => PDO: CURSOR_SCROLL ));
$ Result-> execute ();
$ RecordCount = $ result-> rowCount ();

$ PageCount = ceil ($ RecordCount/$ PageSize );

?>




















$ Pos = ($ Page-1) * $ PageSize; For ($ I = 0; $ I <$ PageSize; $ I ++ ){$ Row = $ result-> fetch (PDO: FETCH_ASSOC, PDO: FETCH_ORI_ABS, $ Pos );If ($ row ){?> $ Pos ++;}?>
Title Content Author Email From

If ($ Page = 1)
Echo "Page 1 ";
Else echo "first page ";
If ($ Page = 1) // set the "previous Page" link
Echo "previous page ";
Else echo "previous page ";
For ($ I = 1; $ I <= $ PageCount; $ I ++) {// Set the link for each page
If ($ I = $ Page) echo "$ I ";
Else echo "$ I ";}
If ($ Page = $ PageCount) // set the "next Page" link
Echo "next page ";
Else echo "next page ";
If ($ Page = $ PageCount) // set the "last Page" link
Echo "last page ";
Else echo "last page ";
Echo "total". $ RecordCount. "records"; // total number of records
Echo "$ Page/$ PageCount Page"; // location of the current Page
?>



For databases that support LIMIT clauses, such as mysql and sqlite, the limit can start with and the length is paginated.
For databases that do not support LIMIT clauses, such as mssql and oracle, you can use ROWNUM and RN to complete paging.
It is said that the higher version of mysql also supports ROWNUM and RN, so the paging code can be much simplified.

For your code
$ Result-> execute (); is used to query all qualified records.
$ Row = $ result-> fetch (PDO: FETCH_ASSOC, PDO: FETCH_ORI_ABS, $ Pos); obtain one of them, and execute it in a loop.
You can
$ Rows = $ result-> fetchall (PDO: FETCH_ASSOC, PDO: FETCH_ORI_ABS, $ Pos );
Retrieve all and
$ Rows = array_chunk ($ rows, $ PageSize );
Cut the number of lines per page into an array, and then
Foreach ($ rows [$ Page-1] as $ row ){
// Output Content
}

The problem is that this sentence cannot get one of them, and the first one is always obtained, that is, PDO: FETCH_ORI_ABS, $ Pos does not work at all.

$ Row = $ result-> fetch (PDO: FETCH_ASSOC, PDO: FETCH_ORI_ABS, $ Pos );

I know that the limit clause is used for paging, but I want to use another method for paging. I like to read all records into the result set and then pagination, which is faster for small result sets.

Your $ Pos on the same page has not changed.

$ Row = $ result-> fetch (PDO: FETCH_ASSOC, PDO: FETCH_ORI_ABS, $ Pos + $ I );

Your $ Pos on the same page has not changed.

$ Row = $ result-> fetch (PDO: FETCH_ASSOC, PDO: FETCH_ORI_ABS, $ Pos + $ I );

You have read the error. After I output one, there is a $ Pos ++;
Of

The problem is that I set $ row = $ result-> fetch (PDO: FETCH_ASSOC, PDO: FETCH_ORI_ABS, $ Pos );
Change
$ Row = $ result-> fetch ();
The results are exactly the same. FETCH_ORI_ABS and $ Pos do not work at all.

PDO: FETCH_ORI_ABS, $ Pos takes effect conditional

$ Result-> fetchall () is better

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.