Re: eating mySQL result rows 1 by 1.. a better way?

From: Date: Thu, 21 Mar 2002 16:20:06 +0000
Subject: Re: eating mySQL result rows 1 by 1.. a better way?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-89528@lists.php.net to get a copy of this message
Hmm.. but if it's not MySql .. but Sybase for instance? What then?.. there is no such thing like LIMIT in SQL "Joffrey Van Wageningen" <joffrey@ne2000.nl> wrote in message news:014801c1d0f2$fd59ac20$0c00a8c0@playcollective.lan... > ----- Original Message ----- > From: "Steve Clay" <steve@mrclay.org> > To: "PHP-GENERAL" <PHP-GENERAL@lists.php.net> > Sent: Thursday, March 21, 2002 4:54 PM > Subject: [PHP] eating mySQL result rows 1 by 1.. a better way? > > > > On my site I paginate query results by limiting rows output to a > > value, say LIMIT, and then the 2nd, 3rd pages run the same query with > > $skip=LIMIT, $skip=(LIMIT*2) value posted back. I use the following > > code to "skip" these result rows, which is just fetching the next row > > to an unused array. > > > > //if there are rows to skip > > if ($result_rows > $rows_to_skip) { > > while ( $rows_to_skip ) { > > // eat a row > > mysql_fetch_array($result); > > $rows_to_skip--; > > $total_results_shown++; > > } > > } > > > > Can I make this more efficient? Is there a way to eliminate this data > > before it leaves the mySQL server (and would it be faster)? > > try 'LIMIT' :) > > http://www.mysql.com/doc/S/E/SELECT.html > > The LIMIT clause can be used to constrain the number of rows returned by the > SELECT statement. LIMIT takes one or two numeric arguments. If two arguments > are given, the first specifies the offset of the first row to return, the > second specifies the maximum number of rows to return. The offset of the > initial row is 0 (not 1): > mysql> select * from table LIMIT 5,10; # Retrieve rows 6-15 > if one argument is given, it indicates the maximum number of rows to return: > mysql> select * from table LIMIT 5; # Retrieve first 5 rows > In other words, LIMIT n is equivalent to LIMIT 0,n. > > Rod Kreisler wrote a nice article on Building Next/Prev Buttons for Query > Results: > http://www.phpbuilder.com/columns/rod20001214.php3 > > hope it helps :) > > mvgr, > Joffrey van Wageningen > > -- > .-[ Joffrey van Wageningen | WoLFjuh | joffrey@ne2000.nl ]-------------- > | Networking Event 2000 - www.ne2000.nl - IRCnet:#ne2000, Undernet:#clue > | PGP:1024D/C6BA5863 - 3B93 52D3 CB91 9CB7 C50D FA79 865F 628A C6BA 5863 > | * We demand guaranteed rigidly defined areas of doubt and uncertainty. > | -- Douglas Adams >

« previous php.general (#89528) next »