0

I'm receiving this syntax error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC'

from this code

 $insert_status=mysql_query("INSERT INTO status(status, date) VALUES
                            ('$status','$date')") or die (mysql_error());
 $get_status= mysql_query("SELECT * FROM status WHERE id DESC")
               or die (mysql_error());
 while($row=mysql_fetch_array($get_status)){
     $status=$row['status'];
     $date=$row['date'];
 }

Mysql version is 5.0.91-community

How can I resolve this small issue? Thanks in advance

6
  • What is id? What does your table schema look like? Commented Apr 2, 2011 at 0:10
  • Hey Greg CREATE TABLE IF NOT EXISTS status ( id int(255) NOT NULL AUTO_INCREMENT, status text NOT NULL, user varchar(255) NOT NULL DEFAULT '', date varchar(255) NOT NULL DEFAULT '', PRIMARY KEY (id) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=31 ; Commented Apr 2, 2011 at 0:10
  • This is like a basic Facebook look alike Newsfeed, the id is the id for each comment. www.fightstar.org/test/wall.php Commented Apr 2, 2011 at 0:12
  • The SELECT statement is corrupt. It's missing a few bits. Name the "id" column should have an operator applied and "DESC" implies it's trying to order the results some way. See my answer below. Commented Apr 2, 2011 at 0:14
  • Thanks Xanadont, I shall give this ago and see what result is given. :) Commented Apr 2, 2011 at 0:16

1 Answer 1

2

WHERE id [...] DESC <- you need something in your where-clause AND you need to clarify what you're ordering on. Something like:

select * from status where id in (1,2,3,4) order by id desc;
Sign up to request clarification or add additional context in comments.

12 Comments

Such as 1 to maybe a 1000? Is there any shorter way than to add each and every number of the amount of comments that may and will be posted into the database under 'id'?
@David Draw: Do you even need a where clause at all?
@David Draw - yes. You can omit the WHERE and use SELECT * FROM status ORDER BY id DESC LIMIT 100 to get the records with highest 100 ids.
It sounds like you may have a lack of understanding exactly what a SELECT statement does. I'm not trying to sound like a jerk, but you may want to learn about SELECT queries first - it looks to me like whatever's generating that SELECT statement (I'm assuming you didn't?) is missing some variables and not filling out the SELECT statement correctly.
Yeah, I was just posting an example of some valid statement that incorporated everything in the original statement. We have no idea exactly what the code's attempting w/o full context.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.