s.k.paul s.k.paul - 1 month ago 4x
MySQL Question

SQLSTATE[42000]: Syntax error or access violation: 1064 in sql select statement

I've just started to learn PHP. What I am trying to do a very simple sql select statement-

$sql = 'SELECT firstname, lastname,email
FROM MyGuests
ORDER BY firstname where id=12';

It gives the following error-

Could not connect to the database testdb :SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;

Sorry if it's a foolish question.


Your query is in the wrong order, order by needs to be after the where.

SELECT firstname, lastname,email
           FROM MyGuests
          where id=12
ORDER BY firstname

You can see the order for all functions in the manual, http://dev.mysql.com/doc/refman/5.7/en/select.html.