1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Getting a previous record from sql table

Discussion in 'PHP' started by qwikad.com, Jun 6, 2018.

  1. #1
    I figure out how I can get the next id using this:
    SEMrush
    $sql_next ="SELECT id FROM table WHERE id > $current_id ORDER BY id
    LIMIT 1";
    if($result = $mysqli->query($query))
    {while($row = $result->fetch_assoc()){
    
    $next = $row['id'];
    
    echo '<a href="https://someurl.com/'.$next.'">Next</a>';
    
    }}
    Code (markup):
    However, I am not sure how to get the previous id. I tried changing WHERE id < $current_id but it didn't do the trick. What should I use to get a previous record from the sql table?
     
    qwikad.com, Jun 6, 2018 IP
    SEMrush
  2. qwikad.com

    qwikad.com Illustrious Member Affiliate Manager

    Messages:
    6,107
    Likes Received:
    1,195
    Best Answers:
    22
    Trophy Points:
    400
    #2
    Adding DESC did the trick (seems to me): WHERE id < $current_id ORDER BY id DESC
     
    qwikad.com, Jun 6, 2018 IP
    sarahk likes this.
  3. Nei

    Nei Well-Known Member

    Messages:
    106
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    170
    #3
    +1 to qwikad.com unswer and also it's better to add additional check for the case if the query result would be empty (if there is no previous record).

    P.S. And by the way there is no need in "while" I suppose...considering "LIMIT 1" in the MySQL query.
     
    Nei, Jun 7, 2018 IP