Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-09-2012, 02:21 AM   PM User | #1
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
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 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 04-09-2012, 04:20 AM   PM User | #2
Coyote6
Regular Coder

 
Join Date: May 2009
Location: Moore, OK
Posts: 277
Thanks: 10
Thanked 41 Times in 41 Posts
Coyote6 is an unknown quantity at this point
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
Coyote6 is offline   Reply With Quote
Old 04-09-2012, 12:49 PM   PM User | #3
myfayt
Senior Coder

 
Join Date: Apr 2010
Posts: 1,156
Thanks: 46
Thanked 95 Times in 94 Posts
myfayt can only hope to improve
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 5 years. My business:
American Made Signs
myfayt is offline   Reply With Quote
Old 04-10-2012, 05:44 AM   PM User | #4
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
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.
__________________
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

Last edited by ShaneC; 04-10-2012 at 05:49 AM..
ShaneC is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:56 PM.


Advertisement
Log in to turn off these ads.