Results 1 to 3 of 3

Thread: Limit MySQL Results

  1. #1
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default Limit MySQL Results

    I'm connecting to a database and pulling results by the date they were added and sorting them accordingly, along with name. This explains the following code:

    PHP Code:
        $result mysql_query("SELECT * FROM standFields ORDER BY dateadded DESC, name ASC");
        echo 
    '<br /><span class="errMsg indent emSmall">Currently sorting by <i style="color:darkred;">date added</i></span><br />'."\n";
        echo 
    '<table cellpadding="5px" cellspacing="0px" class="tableBorder indent"><tr>'."\n";
        unset(
    $standFields['notes']);
        foreach(
    $standFields as $key => $value){ echo "<td><b>$value</b></td>";}
        echo 
    "\n".'</tr>'."\n";
        while(
    $row mysql_fetch_array($resultMYSQL_ASSOC)){
            echo 
    '<tr>';
            foreach(
    $standFields as $key => $value){
                if(
    $key == 'name'){ echo '<td><a href="dEdit.php?viewSN='.$row['sn'].'">'.$row['name'].'</a></td>';}
                else if(
    $key == 'tags'){ echo '<td><a href="dEdit.php?viewSN='.$row['sn'].'">'.$row['tags'].'</a></td>';}
                else{ echo 
    "<td>$row[$key]</td>";}
            }
            echo 
    '</tr>';
        }
        echo 
    '</table>'
    My question is this: how can I alter that while loop to only include, say, the most recent 20 rows, rather than all of them? There are going to be a huge amount of rows and it'd be nice to limit how many are displayed as the most recently added. Thanks.
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    [query].... ORDER BY `field` DESC LIMIT 20;
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    alexjewell (05-27-2010)

  4. #3
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    Oh, well that's much simpler than I thought it'd be. Haha. Thanks!
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

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
  •