note 68568 added to function.db2-fetch-array

From: Date: Wed, 02 Aug 2006 07:26:36 +0000
Subject: note 68568 added to function.db2-fetch-array
Groups: php.notes 
Request: Send a blank email to php-notes+get-115436@lists.php.net to get a copy of this message
i hope this is the best place to post this bit of info. after spending hours searching for an easy way to implement paging of my db2 result set i finally came up with this solution. the paging i am referring to is selecting a subset or limited range of rows from the result set, much as can be done with the LIMIT clause in mysql and postgresql. best of all, this method has limited intrusiveness to the original query string requiring only that we parse out the ORDER BY clause. as an example, assuming that we have a table called MOVIES and the query, $query = "SELECT * FROM MOVIES ORDER BY id"; when executed gives us: ID MOVIE_NAME ----------- ------------------------------ 6 Superman 7 Spiderman 2 8 Batman Begins 9 Matrix Revolution 10 Shrek 2 11 Hoodwinked 12 Ice Age 2 13 Mr. & Mrs. Smith 14 Hostel 9 record(s) selected. and lets assume that we want a page of results consisting of $count = 3 rows, after the first $start = 4 rows from the top. then we can construct a new query from the original one: $query = "(($query) EXCEPT ($query FETCH FIRST $start ROWS ONLY)) " . substr($query, stripos($query, ' ORDER ') . " FETCH FIRST $count ROWS ONLY"; executing this gives: ID MOVIE_NAME ----------- ------------------------------ 10 Shrek 2 11 Hoodwinked 12 Ice Age 2 3 record(s) selected. this ofcourse assumes you are using an ORDER BY clause and not relying on the natural order of the rows, which would otherwise provide inconsistent paging results, and that the ORDER BY clause is at the end of your SQL Query string. hope this helps someone. further, not being a DB2 expert i can't comment on the efficiency of this approach but at first glance it seems fast. God Bless ---- Server IP: 216.194.113.175 Probable Submitter: 65.199.207.186 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.db2-fetch-array.php Edit -- https://master.php.net/note/edit/68568 Del: integrated -- https://master.php.net/note/delete/68568/integrated Del: useless -- https://master.php.net/note/delete/68568/useless Del: bad code -- https://master.php.net/note/delete/68568/bad+code Del: spam -- https://master.php.net/note/delete/68568/spam Del: non-english -- https://master.php.net/note/delete/68568/non-english Del: in docs -- https://master.php.net/note/delete/68568/in+docs Del: other reasons-- https://master.php.net/note/delete/68568 Reject -- https://master.php.net/note/reject/68568 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#115436) next »