Error In My Query?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
SpaceManSpiff
Forum Newbie
Posts: 6
Joined: Sat Apr 27, 2002 8:55 pm

Error In My Query?

Post by SpaceManSpiff »

I'm getting an error with the query below and I can't seem to find anything wrong with it. Can anyone help?

Code: Select all

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate LIMIT 3
Error Message:

Code: Select all

Error 
SQL-query: 

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate LIMIT 3

MySQL said:
It's weird because it doesn't even give a reason for the error after "MySQL said."
User avatar
enygma
Site Admin
Posts: 175
Joined: Fri Apr 19, 2002 8:29 am
Location: Dallas, Tx

Post by enygma »

just be sure that everything is in the right case (MySQL is case sensitive), and, of course, obviously, that all the fields are there.

then, if that still doesn't work right, try running it on the command line MySQL client....and see what that says

-enygma
SpaceManSpiff
Forum Newbie
Posts: 6
Joined: Sat Apr 27, 2002 8:55 pm

Post by SpaceManSpiff »

I tried using that query on phpMyAdmin, that's how I got that error message. All the cases are right and all the fields are there.

Image

The query works fine when I take off "LIMIT 3." The problem occurs when I have that or "LIMIT 0, 3" at the end. Any ideas?
Oxy
Forum Newbie
Posts: 17
Joined: Fri Apr 19, 2002 5:47 am
Location: U.K
Contact:

Post by Oxy »

Code: Select all

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate ASC LIMIT 3
try putting ASC/DESC in between NewsDate and LIMIT
SpaceManSpiff
Forum Newbie
Posts: 6
Joined: Sat Apr 27, 2002 8:55 pm

Post by SpaceManSpiff »

Oxy wrote:

Code: Select all

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate ASC LIMIT 3
try putting ASC/DESC in between NewsDate and LIMIT
I don't understand what that would do, especially since it mixes up different clauses.. but since at this point I'm willing to try anything, it didn't work.

Code: Select all

Error 
SQL-query: 

SELECT NewsID,NewsTitle FROM News ORDER BY NewsDate LIMIT DESC 3
MySQL said: You have an error in your SQL syntax near 'DESC 3' at line 1
Is there any other way to get the three most recent records from a database? I know that in MSSQL, I can do.. "SELECT TOP 3 NewsID,NewsTitle FROM News ORDER BY NewsDate DESC"
Oxy
Forum Newbie
Posts: 17
Joined: Fri Apr 19, 2002 5:47 am
Location: U.K
Contact:

Post by Oxy »

Code: Select all

SELECT * FROM News ORDER BY NewsDate DESC LIMIT 3
<edit>Fixed typo
Last edited by Oxy on Wed May 01, 2002 2:37 pm, edited 1 time in total.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Oxy, no..

Code: Select all

SELECT NewsID,NewsTitle FROM News ORDER BY NewsID DESC LIMIT 3
That is what you want.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Also quick question.

Is there any data in the table?
Oxy
Forum Newbie
Posts: 17
Joined: Fri Apr 19, 2002 5:47 am
Location: U.K
Contact:

yeah

Post by Oxy »

yeah sorry typo, but i'm a bit confused as to why the first query I posted didn't work for you.
SpaceManSpiff
Forum Newbie
Posts: 6
Joined: Sat Apr 27, 2002 8:55 pm

Post by SpaceManSpiff »

jason wrote:Also quick question.

Is there any data in the table?
Yea, there's about five records in there, each having a different NewsDate.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

*sigh*

Code: Select all

SELECT * FROM News ORDER BY NewsID DESC LIMIT 0,3
then, just set $variable = $row['newsID']; or whatever.
User avatar
EvilWalrus
Site Admin
Posts: 209
Joined: Thu Apr 18, 2002 3:21 pm
Location: Springmont, PA USA

Post by EvilWalrus »

phice wrote:*sigh*

Code: Select all

SELECT * FROM News ORDER BY NewsID DESC LIMIT 0,3
then, just set $variable = $row['newsID']; or whatever.
'*' is evil
Post Reply