How to retrieve data in LIFO manner form MySQL???

Discussion in 'MySQL' started by smart, Mar 11, 2008.

  1. #1
    Hi all,

    I have a doubt on how retrieve data form MySQL database in Last-in-First Out manner. I mean to say Recent entry should be on top of the page.


    waiting for reply.................


    Thank you in advance
     
    smart, Mar 11, 2008 IP
  2. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #2
    SELECT * FROM table ORDER BY something [b]DESC[/b]
    Code (markup):
    "something" is usually "ID"...
     
    SoKickIt, Mar 11, 2008 IP
  3. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    It is giving this warning
    "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in"

    This is my code
    $query = "SELECT * FROM datatable ORDER BY Sl.no DESC";
    Sl.no is autoincremented for every entry.
     
    smart, Mar 11, 2008 IP
  4. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Either i use ASC or DESC i am getting the same result. Why????
     
    smart, Mar 11, 2008 IP
  5. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Plese reply
     
    smart, Mar 12, 2008 IP
  6. dizyn

    dizyn Active Member

    Messages:
    251
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    what's your table structure?

    better post few lines form you code form query to fetch array
     
    dizyn, Mar 12, 2008 IP
  7. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #7
    My table has a fields Sl.no, subject, title, url, email, date, text


    $query = "SELECT * FROM datatable ORDER BY 'Sl.no' DESC ";
    $result = mysql_query($query);

    while ($row = mysql_fetch_array($result, MYSQL_NUM))
    {
    if($row != NULL)
    {
    echo "<b>The subject is" .$row[1]. "</b><br>";
    }

    echo "<div class='class2'>";
    echo "<a href='$row[3]' target='_blank'>$row[2]</a></div>";
    echo "<h3>$row[4]$row[5]&nbsp;&nbsp;submitted on $row[6]</h3>";
    echo "<p>$row[7]</p>";

    Is there any thing wrong??
    Either i use DESC or ASC i am getting the same results .
     
    smart, Mar 12, 2008 IP
  8. LinketySplit

    LinketySplit Peon

    Messages:
    97
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Try this:

    SELECT * FROM datatable ORDER BY no DESC

    Is SI referring to a different table? If so, you need to do a join.

     
    LinketySplit, Mar 12, 2008 IP
  9. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #9
    It is giving this warning
    "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in"

    Sl.no is a field in my table.
     
    smart, Mar 12, 2008 IP
  10. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #10
    can't fix it yet. Any solution please???
     
    smart, Mar 13, 2008 IP
  11. mythbuster08

    mythbuster08 Peon

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #11
    
    mysql_query($query);
    
    Code (markup):
    returns not valid mysql resource, but FALSE.
    Catch the error and throw error msg!
    Like: if (!$result) die (mysql_error());
     
    mythbuster08, Mar 13, 2008 IP
  12. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #12
    One solution could be donot use '.' in sl.no

    Other can be change the field name for testing to SRNO

    other can be take another field instead of sl.no and check if it works like change it to subject

    Regards

    Alex
     
    kmap, Mar 13, 2008 IP
  13. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #13
    It is returning valid resource. ASC or DESC nothing is working. Just returns Hower the data is entered into data base.
     
    smart, Mar 13, 2008 IP
  14. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #14
    I tried buddy, Still not working!!!!
     
    smart, Mar 13, 2008 IP
  15. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #15
    Replace:

    $query = "SELECT * FROM datatable ORDER BY 'Sl.no' DESC ";


    with:

    $query = "SELECT * FROM datatable ORDER BY `Sl.no` DESC";
     
    SoKickIt, Mar 14, 2008 IP
  16. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #16
    it gives
    "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in"

    I got to fix this immediately, please help me.
     
    smart, Mar 14, 2008 IP
  17. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #17
    Hey Buddies,

    It is working.....

    Thanks, Thanks, Thanks, Thanks

    A special Thanks to "SoKickIt"
     
    smart, Mar 14, 2008 IP
  18. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Okay, so i want to do this exact same thing, but i only want to show the last three or so entries into the table, not all of them, any way i can do this? Thanks for your help in advance :)
     
    X.Homer.X, Mar 15, 2008 IP
  19. SoKickIt

    SoKickIt Active Member

    Messages:
    305
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    70
    #19
    Just add "LIMIT 3" at the end.
     
    SoKickIt, Mar 15, 2008 IP
  20. X.Homer.X

    X.Homer.X Peon

    Messages:
    290
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #20
    thanks :) i thought that would work, but just wanted to make sure. :p
     
    X.Homer.X, Mar 16, 2008 IP