NEXT 25 records?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
JavaScript
Forum Newbie
Posts: 7
Joined: Tue Jan 28, 2003 8:54 pm

NEXT 25 records?

Post by JavaScript »

how can i display the next 25 records on my mysql table?

The situation is that i need to query and show the table data 25 records per page but don't know how to stop displaying them 25 each per page and continue to next page, next page...
kcomer
Forum Contributor
Posts: 108
Joined: Tue Aug 27, 2002 8:50 am

Post by kcomer »

Code: Select all

SELECT * FROM myCars WHERE color = 'red' LIMIT 26,50
Make LIMIT 0,25 or whatever you are looking for. First number is the start, last is the end.

Keith
JavaScript
Forum Newbie
Posts: 7
Joined: Tue Jan 28, 2003 8:54 pm

Thanks bro!

Post by JavaScript »

But... if using that code, what will happen if we go over our records limit?

For example we have 63 records (and it will increase [i'm coding a news script])

If we limit 0,25 - 26, 50 - 51,76 <<<< We have 63 for now

What will happen?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It will return as many records as it can up to the limit you set but if there are less records than the limit that is not a problem - you're final page will be a bit shorter but there'll be no error caused or anything.

Mac
JavaScript
Forum Newbie
Posts: 7
Joined: Tue Jan 28, 2003 8:54 pm

One more...

Post by JavaScript »

One more issue

How can we know the limit?

In our example, we consider the table contains 63 records but in our productive environment, we don't know where is the stop of our records, do we?

So how can we know where to stop generating a NEXT link as it will put our visitor to no where.


Thanks for all your great helps
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

..without much error control...

Code: Select all

<?php
$rows_per_page = 25;
If (empty($_GET['offset']) || !is_numeric($_GET['offset'])) $offset = 0;
else $offset = (int) $_GET['offset'];
$db = mysql_connect_db('host','user','pass');
mysql_select_db('mydb',$db);

$r = mysql_query("SELECT count(*) FROM mytable  WHERE blah='duh' AND hihaa='beer'",$db);
list($rowcount) = mysql_fetch_row($r);

# Create something to divide the rowcount with $rows_per_page
# and display a bunch of pagelinks with ?offset=nnn etc

$r = mysql_query("SELECT col1,col2,col3 FROM mytable WHERE blah='duh' AND hihaa='beer'
  ORDER BY something,col2 LIMIT ".$offset.','.$rows_per_page,$db);

# Fetch the rows and show them etc...


?>
JavaScript
Forum Newbie
Posts: 7
Joined: Tue Jan 28, 2003 8:54 pm

Beautiful!

Post by JavaScript »

Image

That's what i'm looking for!

I will be back here to show all you guy the news script (GPL)

Thanks :wink:
Post Reply