Here is my query that i want to run in a page to find recent project in a category
"SELECT * FROM project where cat_id=".$category."order by id desc limit 1";
and my project table has following column
id
cat_id
title
year client
description
when i am executing this query in localhost phpMyAdmin panel it runs successfully; but when i am using it on my project its getting following error
Invalid query: 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 'by id desc limit 1' at line 1
Be sure to have a space before the ORDER
"SELECT * FROM project WHERE cat_id=".$category." ORDER BY id DESC LIMIT 1";
try this
make a space before order clause
"SELECT * FROM project where cat_id=".$category." order by id desc limit 1";
Related
I need the MySQL parameter ORDER BY FIELD to order threads from a forum.
Here the exact SQL command: $ids is "19,3,12,256,1023"
SELECT*
FROM
Threads
WHERE
ID IN ($ids)
LIMIT
$untere_schranke, 50
ORDER BY FIELD(ID,$ids)
In phpMyAdmin it works fine but when I use php i get this:
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 FIELD(ID, 19,3,12,256,1023)' at line 15
Order by statement should be before the limit
order by FIELD(ID,$ids)
LIMIT
$untere_schranke, 50
This line of code
$SQL = "SELECT * FROM stats ORDER BY Team WHERE Team='$teamval'";
is returning with the following MySQL 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 'WHERE Team='OTT''
at line 1
I can't find anything wrong with the syntax, what's wrong with it?
The ORDER BY clause must appear after the WHERE clause. So, your query should, instead, be:
SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team
You have used in correct syntax of using order by before where clause , ORDER BY should be used at the end of query if you have used limit in your query then put order by before limit
SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team
Replace:
$SQL="SELECT * FROM stats WHERE Team='$teamval' ORDER BY Team";
I'm trying to get data by an mysqli query.
Query looks like:
SELECT * FROM pxldr_drawings
ORDER BY RAND()
WHERE id NOT IN (1,3,4,2)
LIMIT 1
But i get the following error message
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 'WHERE id NOT IN (1,3,4,2) LIMIT 1' at line 3
I also tried NOT IN ('1','3','4','2') and NOT IN(1,3,4,2), but neither worked.
Thanks, LB
WHERE clause must be before ORDER BY clause.
try this
SELECT * FROM pxldr_drawings
WHERE id NOT IN (1,3,4,2)
ORDER BY RAND()
LIMIT 1
The order of clauses is important, order by should come after where.
SELECT * FROM pxldr_drawings
WHERE id NOT IN (1,3,4,2)
ORDER BY RAND()
LIMIT 1
For the proper syntax of where different clauses need to be placed, please refer to "Select Syntax" documentation.
I am trying to retrieve the top 5 values in a certain column in a mysql database.
I have the following query:
SELECT `dep_aerodrome`,
Count(`dep_aerodrome`) AS `cnt`
FROM sectors
WHERE ( `group` = '".$_SESSION['GROUP']."'
AND ( $bases ) NOT IN dep_aerodrome )
GROUP BY `dep_aerodrome`
ORDER BY `cnt` DESC
LIMIT 5
The query contains a WHERE clause and NOT IN to not include airports that are registered as bases.
I am getting the following 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 'dep_aerodrome) GROUP BY dep_aerodrome ORDER BY cnt DESC LIMIT 5' at line 1
and I cannot figure out where it is coming from. Can anyone help out?
Try this:
SELECT `dep_aerodrome`,
Count(`dep_aerodrome`) AS `cnt`
FROM sectors
WHERE `group` = '".$_SESSION['GROUP']."'
AND ( $bases )
AND dep_aerodrome NOT IN ($some_values)
GROUP BY `dep_aerodrome`
ORDER BY `cnt` DESC
LIMIT 5
I think you want to do this like this. $some_values can be in the format of 'value1','value2',... as in:
dep_aerodrome NOT IN ('value1','value2',...)
The right syntax for using NOT IN is
WHERE column_name NOT IN ('value1','value2',...)
And If you want to match 1 value with multiple columns check this.
Different approach of using IN clause in MySql
I'm making a simple cms system for a site I'm making for non-tech users to edit...
So far so good but when I try and run this code I keep getting: 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 ''pages' ORDER BY 'pages'.'pageOrder' ASC LIMIT 0 , 30' at line 1
By the error it looks like a problem with the order by section and indeed it works without it...
$sql = "SELECT * FROM 'pages' ORDER BY 'pages'.'pageOrder' ASC LIMIT 0 , 30";
$result = mysql_query($sql) or die(mysql_error());
Now I know there is nothing wrong with the code because originally I wrote my own SQL but then after it failed I robbed some from phpmyadmin and it still gives the error but it works in phpmyadmin...
I'm really at my wits end with this, help is very much appreciated thank you...
You shouldn't write 'pages'. Use backticks instead of single quotes for table and column names. Single quotes are used only for strings.
And backticks aren't necessary here anyway. Backticks are generally only required for names that are reserved words in SQL, and names containing special characters or spaces. So you could just do this:
SELECT * FROM pages ORDER BY pageOrder LIMIT 30
The quotes in your query are incorrect. You could either use
$sql = "SELECT * FROM `pages` ORDER BY `pages`.`pageOrder` ASC LIMIT 0 , 30";
if you really need to fully qualify the table/column, or just leave that out and use
$sql = "SELECT * FROM pages ORDER BY pageOrder ASC LIMIT 0 , 30";