SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Balto, MD
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Returning largest ID from MySQL database/sorting

    Let me be brief:

    I am building a chat-style script that will (hopefully) allow people to add their own comments to an ever-growing datapile. The SELECT statement as it is written now will not be efficient enough to handle the size of the database unless I can optimize it.

    Since this chat will allow people to add their own comments in a real-time way, I want to offer people the ability to select how many posts to display on the screen each time they update the page.

    The PHP script also does not always append the new entries to the database. Sometimes they are prepended, sometimes inserted in the middle, etc.

    1. How do I get MySQL/PHP to ONLY append new entries to the database (so they are in numerical order)?
    2. How can I make MySQL reveal the largest value of the unique ID field?

    From #2, I can then subtract the number of messages the user wants to scroll and then do a while {} statement to output only the final XX messages.

    I have tried max(ID) and a couple of other things to address #2, but nothing seems to work. I always get a Resource ID #3 statement instead of a numeral.

    Thanks in advance.

    Jeff

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chances are you are never returning the value from the query:

    Try something like this

    PHP Code:
    $result mysql_qeury("SELECT MAX(ID) as maxid from tablename");
    $maxid mysql_result($result,0,maxid); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Balto, MD
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks Freddie

    I realized after thinking about this problem for a while that I was trying to get the wrong information for what I was trying to do.

    Your code worked brilliantly, but what I wanted was to return the last ten rows from the chat database. The ID and the row number were understandably nowhere near each other in value.

    So I used a mysql_num_rows command and that provided the information I wanted.

    Thanks for your help.

    Jeff


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •