Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,419
    Thanks
    68
    Thanked 102 Times in 101 Posts

    This query isn't limiting

    I am trying to get this query to work, and keep getting an error. If I remove LIMIT 10, it works fine, but how can I add the limit to it?

    PHP Code:
    $Qpost1 = mysql_query("SELECT * FROM posts WHERE id>'0' ORDER BY id DESC LIMIT 10");
    while ($qrow2 = mysql_fetch_array($Qpost1)) {
      ?>
      <tr>
    <td>
    <?php echo "".$qrow2['title'].""?>
    </td>
    <td>
    <?php echo "".$qrow2['category'].""?>
    </td>
    <td>
    <?php echo "".$qrow2['location'].""?>
    </td>
    <td>
    <?php echo "$".$qrow2['price'].""?>
    </td>
    </tr>
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Regular Coder
    Join Date
    May 2009
    Location
    Moore, OK
    Posts
    282
    Thanks
    11
    Thanked 41 Times in 41 Posts
    A little hard to tell without seeing what data you are trying to pull but why do you have the WHERE id>'0'... if your id is auto_incremented when would it not evaluate to true... seems like a waste of database resources... are you going in and changing the id to negative numbers or something? Also its recommended that you just select the specific fields you need from the database and not use *.

    Code:
    SELECT `title`, `category`, `location`, `price`
    FROM `posts`
    ORDER BY `id`
    LIMIT 10

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,419
    Thanks
    68
    Thanked 102 Times in 101 Posts
    I need it to start at the highest id (last inserted) and count 10 backwards to display.
    But also if 10 doesn't exist and it's only 3, then it should only show 3.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    While it shouldn't be strictly necessary, you may want to try LIMIT 0,10.

    LIMIT Syntax: [LIMIT {[offset,] row_count | row_count OFFSET offset}]

    If this doesn't correct your problem, please post the MySQL error you're receiving.
    Last edited by ShaneC; 04-10-2012 at 05:49 AM.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com


  •  

    Posting Permissions

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