user983248
user983248

Reputation: 2688

How can I get first 100 values from database and next 100 and so on?

I have a simple database to store some emails, Is there any way to get the emails from the database in groups of a 100 emails ???

Here is the database structure

Table structure for table `emails`
--

CREATE TABLE IF NOT EXISTS `emails` (
  `email` varchar(100) NOT NULL,
  `ip` varchar(15) NOT NULL,
  `timestamp` datetime NOT NULL,
  PRIMARY KEY  (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

The ideal outcome would be to get the first 100 emails, do something and later get the next 100 and do something else.

Thanks and sorry for my english

Upvotes: 2

Views: 1203

Answers (2)

hafichuk
hafichuk

Reputation: 10781

Use the SQL LIMIT <offset>, <row_count> syntax to paginate via SQL.

Assuming that you are going to be displaying these results on a webpage, I'd also recommend that you use a PHP library like PEAR::Pager to help you out here as well. There is a good tutorials here.

One thing to note, for performance reasons, you should ensure that you are using ORDER BY in your paginated query and that you are using an index. The MySQL Performance blog has a good explanation.

Upvotes: 0

Brad
Brad

Reputation: 163232

LIMIT 100

http://dev.mysql.com/doc/refman/5.1/en/select.html#id827984

Then for the next one, LIMIT 100, 100, and then LIMIT 200, 100, and so on.

The first number (when there are two) is the offset.

Upvotes: 5

Related Questions