Paging with PHP/MySQL?

Leviathan

[span="font-family:ubuntu; color: whitesmoke; padd
  • 1,103
    Posts
    11
    Years
    Say, anyone familiar with PHP and the MySQL API? I've got a lil' problem on my hands that I can't remember how to do.

    Basically, it's paging. And I'm trying to retrieve a string value from a row in a table whose primary is one greater than the record being viewed.

    Sooo if anyone can direct me to some handy pagination code, that'd be great!
     
    Gonna move this into its own thread since it appears to be a larger issue at hand, and since the DCC thread is mainly for small talk/rolling discussion. :)
     
    Is there no chance for gaps in your data? Are you always retrieving a single value that is one higher (or lower) than the current one? This is fairly simple then:

    Assuming you have the current ID stored in $id (and you're properly sanitized it with something like intval() when setting it):
    Code:
    SELECT string_col FROM table WHERE id = $id + 1;

    If you don't know that $id + 1 exists, you can do:
    Code:
    SELECT string_col FROM table WHERE id > $id ORDER BY id LIMIT 1;

    And the same applies in reverse if you want to go backwards.

    I feel like this is too simple and I'm missing something though. Let me know and I can provide more detailed code samples, especially if you tell me how you're interfacing with the database (the old mysql commands, mysqli, pdo, etc.).
     
    LIMIT and OFFSET are used in my more specific examples but the OP seemed to state that only a single row needed to be returned so it's not "paging" in the traditional sense; it sounds more like browsing through individual items in a collection which is conceptually a bit different.
     
    In more general terms, you can also refer to the LIMIT clause which is very useful for simple paginations.

    Ah W3Schools my fickle friend, we meet again.
     
    Back
    Top