note 108033 added to pdo.prepare
| From: | gjarrige at six-axe dot fr | Date: | Fri, 23 Mar 2012 13:57:52 +0000 |
| Subject: | note 108033 added to pdo.prepare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186790@lists.php.net to get a copy of this message | ||
I found a bug with the PDO Scroll Cursor technique, for which I posted a note the 08-Mar-2012 at
12:00.
I found a solution to circumvent the problem (2 patchs in the code below) :
public static function getScrollCursor($db, $sql, $args, $offset, $nbl_by_page ) {
if (!is_array($args)) {
$args = array() ;
}
$offset = intval($offset) ;
if ($offset <= 0) {
$offset = 1 ;
}
/* patch 1 :
* L'affichage doit démarrer sur l'offset -1, sinon on "rate" la
première ligne
* the decrement is necessary not to miss the first line of the result set
*/
$offset-- ;
$nbl_by_page = intval($nbl_by_page);
if ($nbl_by_page <= 0) {
$nbl_by_page = 10 ;
}
$sql = trim ($sql );
$rows = array ();
try {
$st = $db->prepare ( $sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$st->execute ( $args );
if ($offset > 0) {
/* patch 2 :
* Un bug d'origine inconnu oblige à effectuer un premier positionnement
* sur la ligne n° 0, quand on affiche les offsets > 0
* Dans le cas où on affiche l'offset 0, il ne faut surtout pas faire
* ce premier positionnement, car il interfère avec celui qui est
* effecuté par la boucle d'affichage (for).
* !! Very important patch here, for $offset > 0 only (not for $offset = 0)
*/
$lost = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, 0);
}
for (
$tofetch = $nbl_by_page,
$row = $st->fetch(PDO::FETCH_ASSOC, PDO::FETCH_ORI_REL, $offset);
$row !== false && $tofetch-- > 0;
$row = $st->fetch(PDO::FETCH_ASSOC)
) {
$rows [] = $row ;
}
unset ( $st );
return $rows;
} catch ( Exception $e ) {
// place your error code here;
}
}
----
Server IP: 195.221.21.36
Probable Submitter: 212.155.199.228
----
Manual Page -- http://www.php.net/manual/en/pdo.prepare.php
Edit -- https://master.php.net/note/edit/108033
Del: integrated -- https://master.php.net/note/delete/108033/integrated
Del: useless -- https://master.php.net/note/delete/108033/useless
Del: bad code -- https://master.php.net/note/delete/108033/bad+code
Del: spam -- https://master.php.net/note/delete/108033/spam
Del: non-english -- https://master.php.net/note/delete/108033/non-english
Del: in docs -- https://master.php.net/note/delete/108033/in+docs
Del: other reasons-- https://master.php.net/note/delete/108033
Reject -- https://master.php.net/note/reject/108033
Search -- https://master.php.net/manage/user-notes.php