![]() |
MySQL Error with Pagination
When trying to perform a sql query with a normal pagination procedure, I get this error:
PHP Code:
|
Try it without quotes around news
|
`news` should be fine.
SQL Code:
PHP Code:
|
The problem is your understanding of the
LIMIT clause. Here's some examples with your query as it is at the moment.WHERE p = 1, max = 3, limit = (1 - 1) * 3:The syntax for specifying limits is as follows: SELECT ... LIMIT [offset,] row_countPutting that in terms of what you want to be using: SELECT ... LIMIT $limit, $maxYour variable naming doesn't make things very clear ( $limit isn't really the limit, $max is). So, I'd suggest perhaps adopting some more suitable variables, like:$page (the current page, eg. 1: replaces $p)$perpage (how many items per page, eg. 3: replaces $max)$offset (the row offset to use in the query, eg. 3 for page 2: replaces $limit)Then you query can become: SELECT * FROM news LIMIT $offset, $perpage |
Heres the whole thing I made:
PHP Code:
|
Your math wont work on page or 1
PHP Code:
Salathe explained the limit comman well. But to answer your specific question, you cant limit to a negative number like that. Although your paganation script wont work with the query you have at the first post </span></span> |
Quote:
Could you guys make a tutorial on this? I'm not good with math when using MySQL. :S |
The generic code I use, form an image hosting site I built
PHP Code:
|
Quote:
|
Pagination is frustrating. >.<
Update: So far, it's still buggy. :/ If you put in /?page=1 it shows it all, if you put in 2 it shows maximum 3 is how I want it. -_- |
I've tried 5 tutorials, none work. Heh, for me, I'm starting to hate this. -_-
|
Quote:
PHP Code:
|
Hey, I like that class. I have something like that in a bit of a scattered procedural form. Thought about turning it into a class, after seeing your first reply and thinking about my own project.
One question though, if you'll humor me. Why write this instead of the equivalent shorthand? Is the first faster, and if so why? PHP Code:
|
In case I ever want to change anything in the way it passes the get data on for a specific purpose. Just leaves the door open so I wont have to make any more modifications then I have to.
As for speed, I dont know, but the difference would be negligible at best. |
I lack MySQL skills, so I'll just keep trying till I at least get something right. :P
|
I was wondering, since the only reason I could figure was either flexibility or speed. And I doubted the latter was the one. Exactly why I asked. ;)
|
I'm used to postgres which has LIMIT and OFFSET clauses.
Did you get it sorted out orc? |
No, though I got 1 and Next up, and I put I got prev working, though it's stuck on page 1 so it doesnt show prev, but I still cannot get the query right. :[ I have two queries, one of them is count and the other is the regular query which displays all the news. If I try to put a limit with that using $max or $num ( $page - $max * $max ) then I would get an error, I looked at the error using mysql_error(), and nothing is there. so yeah, it's not working :P
Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '''' at line 1Code:
quotes . foo,bar . quotesCode:
noquotes foo,bar noquotes |
I'll run through a way I do it, but I'm using postgres so the SQL is slightly different. This probably isn't the best way by any means, but it works for me.
I hope this is of some help. I wouldn't be suprised if I made a mistake above. Sorry about the weird 1,1,1 list! |
I'm always getting a negative number!! '-3,3'
PHP Code:
|
| All times are GMT. The time now is 07:41 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0