473,543 Members | 4,392 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

LIMIT clause problems

New to PHP and MySQL.
Using PHP5 and MySQL 4.1
Windows XP Pro
IIS 5.1

I'm trying to page a recordset, and am using a LIMIT clause to fetch a
defined range of records from my db. However, the returned dataset is not
limited to the range I have in the SQL clause.

Here's the code:

<?php
$link_id=mysql_ connect("localh ost", "username", "password") ;
mysql_select_db ("stories", $link_id);
global $records_per_pa ge;

$records_per_pa ge = 5;
$query = "SELECT StoryID FROM Stories";
$result = mysql_query($qu ery);
$total_recs = mysql_num_rows( $result);

if(empty($_GET['next_page'])) {
$_GET['next_page'] = 0;
}

$cur_page = $_GET['next_page'];
$page_num = $cur_page + 1;
$record = ($cur_page * $records_per_pa ge) + 5;
$total_num_page = $last_page_num = ceil($total_rec s/
$records_per_pa ge);

$limit_str = "LIMIT ". $cur_page * $records_per_pa ge . ", $record";
$query = "SELECT StoryID, Headline FROM STORIES ORDER BY Headline
$limit_str ";
$result = mysql_query($qu ery);

while($query_da ta = mysql_fetch_arr ay($result)) {
$story_id = $query_data["StoryID"];
$headline = $query_data["Headline"];
echo "<A href=\"story.ph p?id=$story_id\ ">$headline </a><br>\n";
}

if($page_num > 1) {
$prev_page = $cur_page - 1;
echo "<A href=\"paging.p hp?next_page=0\ ">[Top]</a>";
echo "<A href=\"paging.p hp?next_page=$p rev_page\">[Prev]</a>";
}

if ($page_num < $total_num_page ) {
$next_page = $cur_page + 1;
$last_page = $total_num_page - 1;
echo "<A href=\"paging.p hp?next_page=$n ext_page\">[Next]</a>";
echo "<A href=\"paging.p hp?next_page=$l ast_page\">[Last]</a>";
}

echo "<br>" . $limit_str

?>

I echo out the Limit clause, and it gives me what I would expect to see on
each page, but the actual number of records returned increases by 10 for
each page I navigate.

Interestingly, when I try the SQL in MySQL Query Browser, it seems to
ignore the upper boundary in the LIMIT clause.

I'm probably being a total noddy, but can anyone see what I've done wrong?

TIA
Mike
Aug 6 '05 #1
2 1745
On Sat, 06 Aug 2005 10:42:33 GMT, Mike <us**@domain.co m> wrote:
$cur_page = $_GET['next_page'];
$page_num = $cur_page + 1;
$record = ($cur_page * $records_per_pa ge) + 5;
$total_num_page = $last_page_num = ceil($total_rec s/
$records_per_p age);

$limit_str = "LIMIT ". $cur_page * $records_per_pa ge . ", $record";


http://dev.mysql.com/doc/mysql/en/select.html
[LIMIT {[offset,] row_count | row_count OFFSET offset}]

It's not LIMIT from_offset, to_offset.
it's LIMIT offset, row_count.

So surely something more like:

$limit_str = "LIMIT ". $cur_page * $records_per_pa ge . ", $records_per_pa ge";

--
Andy Hassall / <an**@andyh.co. uk> / <http://www.andyh.co.uk >
<http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool
Aug 6 '05 #2
Andy Hassall <an**@andyh.co. uk> wrote in
news:29******** *************** *********@4ax.c om:
On Sat, 06 Aug 2005 10:42:33 GMT, Mike <us**@domain.co m> wrote:
$cur_page = $_GET['next_page'];
$page_num = $cur_page + 1;
$record = ($cur_page * $records_per_pa ge) + 5;
$total_num_page = $last_page_num = ceil($total_rec s/
$records_per_ page);

$limit_str = "LIMIT ". $cur_page * $records_per_pa ge . ",
$record";


http://dev.mysql.com/doc/mysql/en/select.html
[LIMIT {[offset,] row_count | row_count OFFSET offset}]

It's not LIMIT from_offset, to_offset.
it's LIMIT offset, row_count.

So surely something more like:

$limit_str = "LIMIT ". $cur_page * $records_per_pa ge . ",
$records_per_pa ge";


Yep, that works fine. Thanks.

The book I'm using wasn't clear how to use the LIMIT clause, and it
looked like it was from_offset, to_offset. The sample code provided
*definitely* produces from_offset, to_offset.

Wrox are normally very good, but "Beginning PHP5" is not up to standard.
I've noticed quite a few typos in sample code.

I'll just have to make more use of the official documentation.

Mike

Mike
Aug 6 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
17074
by: Tamer Higazi | last post by:
Hi! I have problems creating joins between tables and to limit rows for a specified clolumn. I have 2 tables! employees departments I face the problems with the GROUP BY clause and i don't get it handled solving that problem. Neither with oracle sql or SQL1999 syntax!
10
14024
by: Randell D. | last post by:
Folks, I have a SELECT that returns with multiple records - It works when I have a LIMIT clause but when I try to include a GROUP BY clause, the select returns nothing (ie no records, no errors). The GROUP BY specifies a column that does exist in my db (and is also the only field that is select from the db). Where am I going wrong? ...
0
575
by: Fatso | last post by:
When I execute the following query without the LIMIT clause, it executes under 1 second. When I add the LIMIT clause, it takes 10 seconds to complete. The client table contains 100,000 records. What is going on? SELECT * FROM clients WHERE rep_id = 1 AND last_name LIKE '%au%' ORDER BY first_name DESC
3
2601
by: Jay K | last post by:
Hi, I have multiple queries like this: SELECT col1, col2, col3, col4 FROM table1, table2 where table1.col1 = table2.col1 and table1.col2 = 1 ORDER BY col3 desc LIMIT 5 and
5
2100
by: Jeremy | last post by:
I am relatively inexperienced with SQL, and I am trying to learn how to analyze some data with it. I have a table with the following information. COMPANY ID , DATE, MarektValue I would like to select the largest 100 marketvalues for each date in my table. How can I do this efficiently in MySql.
2
18094
by: Urban | last post by:
hi, I have a question. Maybe You know the equivalent to command LIMIT from MySQL I couldn`t find something like this in MS SQL PS I try to display 10 records begining form e.g. 4 sort by id something like: "SELECT * FROM table WHERE name=... LIMIT 4, 10 ORDER BY id" in MySQL
1
2315
by: lawrence k | last post by:
Want to replace the limit clause in a query, but can't get it right. What's wrong with this: $pattern = "(.*)limit (.*)"; $replacement = '$1'; $replacement .= "LIMIT $limit"; $replacement .= '$2'; $query = preg_replace ($pattern, $replacement, $query);
1
22562
by: henryrhenryr | last post by:
Hello! I'm having a bit of trouble with a query. I am trying to paginate a set of results using LIMIT. Each result has a bit of 'basic' data and potentially multiple 'additional' data. I have used LEFT JOIN to get the data from table 2. I want to limit the amount of data to 10 rows of table 1 but if there are two pieces of data on table 2...
1
14874
by: woony | last post by:
I am making a webpage that gets his information from an as400 system. Now I seemed to hit some weird problem. I have for example this select: select field1,field2 from table this is over 25.000 results. and works perfectly now the second I add a where clause
0
7402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7733
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5264
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4890
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3388
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1814
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
633
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.