Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. PDO Get Previous and Next Record partially working

PDO Get Previous and Next Record partially working

Scheduled Pinned Locked Moved Database
databasemysqlhelp
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    needAbreakNow
    wrote on last edited by
    #1

    I'm trying to fetch the previous and next record using PDO/MYSQL. The SQL code for Next fetches the next record, but for Previous always returns the first record. Don't understand why Previous does not work. There are 7 records in this test set and the 'article_id' field is not contiguous (due to deletions) but is broken up as such: 24, 45,46,47, 48, 50, 51. Code below:

        $id = 47;
        echo 'current id is:'.$id;
        //GET PREVIOUS ID
        $stmt= $db->prepare("SELECT \* FROM blog WHERE article\_id<$id LIMIT 0,1");
        $stmt->execute();
        $row=$stmt->fetch(PDO::FETCH\_ASSOC);
        echo '  
    

    ';
    echo 'Previous ID is:'.$row['article_id'];
    //GET NEXT ID
    $stmt= $db->prepare("SELECT * FROM blog WHERE article_id>$id LIMIT 0,1");
    $stmt->execute();
    $row=$stmt->fetch(PDO::FETCH_ASSOC);
    echo '
    ';
    echo 'Next ID is:'.$row['article_id'];

    The resultant output is:

     current id is:47
     Previous ID is:24
     Next ID is:48
    

    The previous ID should be 46, not 24! I've tried the same SQL in phpMyAdmin directly and it still returns 24 for the previous, but works fine for the next. I've seen many other examples on the web showing the similar code as I have, but theirs seems to work (via youtube), mine doesn't. Any help would be appreciated! Thanks in advance.

    L V N 3 Replies Last reply
    0
    • N needAbreakNow

      I'm trying to fetch the previous and next record using PDO/MYSQL. The SQL code for Next fetches the next record, but for Previous always returns the first record. Don't understand why Previous does not work. There are 7 records in this test set and the 'article_id' field is not contiguous (due to deletions) but is broken up as such: 24, 45,46,47, 48, 50, 51. Code below:

          $id = 47;
          echo 'current id is:'.$id;
          //GET PREVIOUS ID
          $stmt= $db->prepare("SELECT \* FROM blog WHERE article\_id<$id LIMIT 0,1");
          $stmt->execute();
          $row=$stmt->fetch(PDO::FETCH\_ASSOC);
          echo '  
      

      ';
      echo 'Previous ID is:'.$row['article_id'];
      //GET NEXT ID
      $stmt= $db->prepare("SELECT * FROM blog WHERE article_id>$id LIMIT 0,1");
      $stmt->execute();
      $row=$stmt->fetch(PDO::FETCH_ASSOC);
      echo '
      ';
      echo 'Next ID is:'.$row['article_id'];

      The resultant output is:

       current id is:47
       Previous ID is:24
       Next ID is:48
      

      The previous ID should be 46, not 24! I've tried the same SQL in phpMyAdmin directly and it still returns 24 for the previous, but works fine for the next. I've seen many other examples on the web showing the similar code as I have, but theirs seems to work (via youtube), mine doesn't. Any help would be appreciated! Thanks in advance.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I am not a SQL expert, but the following occurs to me. Your SELECT statement says: find all records whose article_id is less than the value given (i.e. 47), and return the first record found. So SQL starts searching the table at the beginning, checks the first record and its id is 24, and that matches your search criteria. In order to do what you want you need somehow to tell it to find the record with the highest id that is less than 47. But I am not sure how you would specify that.

      1 Reply Last reply
      0
      • N needAbreakNow

        I'm trying to fetch the previous and next record using PDO/MYSQL. The SQL code for Next fetches the next record, but for Previous always returns the first record. Don't understand why Previous does not work. There are 7 records in this test set and the 'article_id' field is not contiguous (due to deletions) but is broken up as such: 24, 45,46,47, 48, 50, 51. Code below:

            $id = 47;
            echo 'current id is:'.$id;
            //GET PREVIOUS ID
            $stmt= $db->prepare("SELECT \* FROM blog WHERE article\_id<$id LIMIT 0,1");
            $stmt->execute();
            $row=$stmt->fetch(PDO::FETCH\_ASSOC);
            echo '  
        

        ';
        echo 'Previous ID is:'.$row['article_id'];
        //GET NEXT ID
        $stmt= $db->prepare("SELECT * FROM blog WHERE article_id>$id LIMIT 0,1");
        $stmt->execute();
        $row=$stmt->fetch(PDO::FETCH_ASSOC);
        echo '
        ';
        echo 'Next ID is:'.$row['article_id'];

        The resultant output is:

         current id is:47
         Previous ID is:24
         Next ID is:48
        

        The previous ID should be 46, not 24! I've tried the same SQL in phpMyAdmin directly and it still returns 24 for the previous, but works fine for the next. I've seen many other examples on the web showing the similar code as I have, but theirs seems to work (via youtube), mine doesn't. Any help would be appreciated! Thanks in advance.

        V Offline
        V Offline
        Victor Nijegorodov
        wrote on last edited by
        #3

        To extend Richard's reply: you may want to refine your SQL query using ORDER BY clause or/and MAX / MIN functions.

        1 Reply Last reply
        0
        • N needAbreakNow

          I'm trying to fetch the previous and next record using PDO/MYSQL. The SQL code for Next fetches the next record, but for Previous always returns the first record. Don't understand why Previous does not work. There are 7 records in this test set and the 'article_id' field is not contiguous (due to deletions) but is broken up as such: 24, 45,46,47, 48, 50, 51. Code below:

              $id = 47;
              echo 'current id is:'.$id;
              //GET PREVIOUS ID
              $stmt= $db->prepare("SELECT \* FROM blog WHERE article\_id<$id LIMIT 0,1");
              $stmt->execute();
              $row=$stmt->fetch(PDO::FETCH\_ASSOC);
              echo '  
          

          ';
          echo 'Previous ID is:'.$row['article_id'];
          //GET NEXT ID
          $stmt= $db->prepare("SELECT * FROM blog WHERE article_id>$id LIMIT 0,1");
          $stmt->execute();
          $row=$stmt->fetch(PDO::FETCH_ASSOC);
          echo '
          ';
          echo 'Next ID is:'.$row['article_id'];

          The resultant output is:

           current id is:47
           Previous ID is:24
           Next ID is:48
          

          The previous ID should be 46, not 24! I've tried the same SQL in phpMyAdmin directly and it still returns 24 for the previous, but works fine for the next. I've seen many other examples on the web showing the similar code as I have, but theirs seems to work (via youtube), mine doesn't. Any help would be appreciated! Thanks in advance.

          N Offline
          N Offline
          needAbreakNow
          wrote on last edited by
          #4

          Solved the issue, and thanks for the hints!

          $stmt= $db->prepare("SELECT * FROM blog WHERE article_id<$id ORDER by article_id DESC LIMIT 0,1");

          It seems like the for the next record, the system naturally assumes ascending, but for the previous, I had to put in the DESC order in!

          L 1 Reply Last reply
          0
          • N needAbreakNow

            Solved the issue, and thanks for the hints!

            $stmt= $db->prepare("SELECT * FROM blog WHERE article_id<$id ORDER by article_id DESC LIMIT 0,1");

            It seems like the for the next record, the system naturally assumes ascending, but for the previous, I had to put in the DESC order in!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            I think that Ascending is the default order for all SELECT clauses.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups