Mysql Error in fields list [duplicate] - php

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
This is my list of fields:
$fields = "ID, PropertyID, DateAdded, DateUpdated, RegionName, PropertyType, OtherType, SaleType, Condition, Price, CurrencyName, Title, MainImage, Summary, NoOfBeds, NoOfBaths, NoOfReceptionRooms, Floor, FloorSpace, Furnished, Pool, Garden, CoveredArea, MeasureUnit, Parking, DistanceSea, DistanceAirport, DistanceGolf, DetailNotes, FeaturedProperty, Sold, vrTour, Outbuildings";
And for some strange reason I am getting this 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 'Condition, Price, CurrencyName, Title, MainImage, Summary, NoOfBeds, NoOfBaths, ' at line 1
Has anyone ever come accross a problem in the field names, I have ages ago but can't remember how I solved it, and hey, I didn't know about SO then.

Condition
condition is the mysql reserved word use backquote to avoid error
`Condition`
Here is the list of mysql reserved words
http://dev.mysql.com/doc/refman/5.1/en/reserved-words.html

Condition is a reserved keyword in mysql. If you really want to use it as a field name, you have to wrap it in backticks.

"Condition" is a MySQL reserved word and such needs to be used in backquotes.

Related

SQL syntax error with no known mistakes in query [duplicate]

This question already has answers here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Closed 5 years ago.
I'm getting a syntax error for a very simple SQL query I'm trying to do:
INSERT INTO history (character, type, amount, extra)
VALUES('$character', '$type', '$amount', '$extra')
Here's the way the table is set up:
SQL table
The full error it gives me is the following:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'character, type, amount, extra) VALUES('Ellie', 'Gift', '-200', 'to Rick')' at line 1
I've already checked and double checked the usual mistakes like the table name, spelling errors, column order etc, but I'm clueless as to what it's still detecting, and hoping one of you can help me out...
character is a reserved keyword in mysql. Rename the column or use backticks for escaping it.
INSERT INTO history (`character`, type, amount, extra)
VALUES('$character', '$type', $amount, '$extra')

PHP insert into SQL statement with several parameters [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
i have a simple php INSERT INTO SQL statement that simply refuses to update several columns at once. i have no idea why but the following statement is acceptabel;
$sql = "INSERT INTO niceTable (first) VALUES ('Hello')";
however if i try to following
$sql = "INSERT INTO niceTable (first, last) VALUES ('Hello', 'You')";
it breaks down and throws the following error:
"Error updating record: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'desc) VALUES ('Hello', 'update')' at line 1"
I have checked the syntax, but it seems ok. I am using a one.com server. Anyone got any tips?
Your actual query (not the one in your question) seems different. The error message seems to have desc somewhere, which is a reserved word. If you use reserve words as column names (don't), you should enclose them in backticks:
INSERT INTO tbl (`order`, `desc`) VALUES ('foo', 'bar');
As per your "posted code":
The reason being that first and last are MySQL reserved words
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
and require special attention.
Either wrap them in ticks or rename them to something other than reserved keywords.
INSERT INTO niceTable (`first`, `last`)
Edit: However, your error doesn't support the issue here, nor the column name(s):
for the right syntax to use near 'desc)
this tells me you are using desc which is also another MySQL reserved word.
You should also use prepared statements
https://en.wikipedia.org/wiki/Prepared_statement
Plus, should your inputs contain characters that MySQL may complain about such as apostrophes John O'Neil then you will need to escape those values.
MySQL will interpret that as ('Hello', 'John O'Neil') in turn causing another syntax error.
Escaping it, would interpret it as ('Hello', 'John O\'Neil') making it valid.
I'm thinking ahead here.
Enclose your column names in backticks
Last is a function in MySQL
$sql = "INSERT INTO niceTable (`first`, `last`) VALUES ('Hello', 'You')";

Insert single numerical value into MySQL column [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
The query I'm trying to run is:
INSERT INTO albumtest (on) VALUES ('3')
Server type: MySQL
Server version: 5.6.21
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 'on) VALUES ('3')' at line 1
I've been looking over this for almost a day now, and I can't seem to figure out why I'm getting this syntax error.
Full code is here: http://pastebin.com/6mMbZ1Y1
But I know the rest of it is fine, because it can run other queries fine (such as $sql = "INSERT INTO gallery (title) VALUES ('".$title."')";)
this is the correct syntax
INSERT INTO `albumtest` (`on`) VALUES ('3')
ON is reserved keyword of mysql it should be write inside backtics
INSERT INTO albumtest (`on`) VALUES ('3')
on is MySql reserved keyword. Dont use it as column name.

SQL/PHP INSERT error [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
I am trying to store form input into SQL database via PHP. When I submitted the form, I received 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 'Describe, Living, City, State, Major) VALUES ('First', 'Last', 'Company ' at line 1".
Here is my PHP:
$insert_sql="INSERT INTO Interns (First, Last, Company, Description,Classes, Interview, Projects, Benefits, Describe, Living, City, State, Major) VALUES ('$first_name', '$last_name', '$company', '$description', '$classes', '$interview', '$projects','$benefits', '$describe', '$living', '$city','$state','$major');";
describe is a reserved word in MySQL and needs to be escaped with backticks.
INSERT INTO Interns (..., Benefits, `Describe`, Living, ...
You are using a reserved keyword Describe
You need to escape it using backticks in query as
INSERT INTO Interns
(
First,
Last,
Company,
Description,
Classes,
Interview,
Projects,
Benefits,
`Describe`,
..............

simple mysql select error limit [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 8 years ago.
This is the error message and my code. I just don't see the error.
Description:
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 'key='cbd1f3bb822e8617b624301774287490d3fcd97e' LIMIT 1' at line 1
Query:
SELECT *
FROM wp_wpsc_api_keys
WHERE name='MichelleAllen17'
AND key='cbd1f3bb822e8617b624301774287490d3fcd97e'
LIMIT 1
Any ideas of what can be the issue in my sql are welcome
KEY is a reserved keyword, it must be escaped with backtick.
SELECT *
FROM wp_wpsc_api_keys
WHERE name = 'MichelleAllen17' AND
`key` = 'cbd1f3bb822e8617b624301774287490d3fcd97e'
LIMIT 1
MySQL Reserved Keywords
put backtiks around the field names
...where `name`=... `key`
As an alternative to backticks, another "best practice" pattern is to QUALIFY all column names with the table_name, or a convenient table alias, e.g.
SELECT t.*
FROM wp_wpsc_api_keys t
WHERE t.name='MichelleAllen17'
AND t.key='cbd1f3bb822e8617b624301774287490d3fcd97e'
LIMIT 1
This prevents MySQL from seeing the column name "key" as a reserved word.
Let's be clear: the problem in your query isn't a lack of backticks... the problem is that MySQL is seeing a token in your query text (in this case "key") as a reserved word, rather than as the name of the column. The solution is to prevent MySQL from seeing that token as a keyword. Using backticks is one way to accomplish that, but they aren't required.
Using backticks is entirely valid, and can be done along with qualifying the column names. The backicks are required when the column name contains spaces or special characters. Here is the same query, with the table and column names enclosed in backticks:
SELECT t.*
FROM `wp_wpsc_api_keys` t
WHERE t.`name`='MichelleAllen17'
AND t.`key`='cbd1f3bb822e8617b624301774287490d3fcd97e'
LIMIT 1
I just happen to find it annoying to have to look at, or type, backticks that are unnecessary. It is MUCH MORE useful use of keystrokes (for me) to have the column names qualified ("t."), even if that isn't required, just because I am SO used to seeing column names qualified whenever there is more than one table in a query (which happens a LOT for a lot of really useful queries.)

Categories