note 108033 deleted from pdo.prepare by danbrown

From: Date: Thu, 10 May 2012 17:55:34 +0000
Subject: note 108033 deleted from pdo.prepare by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-187686@lists.php.net to get a copy of this message
Note Submitter: gjarrige at six-axe dot fr ---- 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; } }

« previous php.notes (#187686) next »