Need help with 2 MySQL/PHP Questions

Discussion in 'PHP' started by Imozeb, May 24, 2010.

  1. #1
    1. How do I select the last 20 rows of a table in my database?
    2. Is there anyway in MySQL or PHP to only query a database if the data in a table has changed?

    Thanks.
     
    Imozeb, May 24, 2010 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    1. SELECT columns FROM my_table ORDER BY id DESC LIMIT 20;

    If you have a auto-increment or other sequential id column or a datetime/timestamp column, you would use that column in the order by.

    2. You should use caching to prevent the database from performing a full query every time.
    http://www.mysqlperformanceblog.com/2006/07/27/mysql-query-cache/

    If you are using php, you can also use a front-end caching mechanism like apc which can completely eliminate successive-loads on the database.
     
    jestep, May 24, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I'm trying to get question 1. to work but it isn't working (It only outputs 1 row)

    This what my order by statement looks like. I'm trying to get 10 of the most recent rows.
    ORDER BY STR_TO_DATE(`date`, "%m/%d/%Y") DESC, `time` DESC LIMIT 10
    Code (markup):
    Why is it not working? Thanks.

    Never Mind I got it.
     
    Last edited: May 24, 2010
    Imozeb, May 24, 2010 IP
  4. Urlin

    Urlin Member

    Messages:
    29
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    May be:
    You can use any variable (not 20 constant):
     
    Urlin, May 29, 2010 IP