views:

70

answers:

4

This is a query I am doing with mysql using PHP
This is the query line

<?php
$query = "SELECT * FROM node WHERE type = 'student_report' AND uid = '{$uid}' LIMIT 1 ORDER BY created DESC";
?>

I get the following 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 'ORDER BY created DESC' at line 1
+1  A: 

You don't need the curly braces around $uid

If that does not solve it, try putting the LIMIT clause after the order by clause, which is the recommanded way. Source Mysql select syntax.

codaddict
I removed the curly braces still getting the same error. Actually it works if I remove $uid and put a number. But I need to use the variable
Anytime
Looks like your $uid is not getting populated correctly before you run the query. Try echoing the value of $uid just before the query and ensure it has got some meaningful value in it.
codaddict
$uid has a value of 15
Anytime
+3  A: 

You need to have the limit clause last.

Ruggs
A: 

print the entire sql query ($query), not only $uid and also the LIMIT clause after order by

Shamly
+1  A: 

Your query should be:

$query = "SELECT * FROM node WHERE type = 'student_report' AND uid = $uid ORDER BY created DESC LIMIT 1";
jwhat