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 6 years ago.
I have a simple update query, but it is not working:
update user set active = 'Y' , delete = 'N' where id = 1;// not working
but if I add a special character which phpmyadmin uses then it is working
update `user` set `active` = 'Y' , `delete` = 'N' where `id` = 1;//its working but its database generated
and there is no difference except the ` special character which is not mandatory.
delete is MySQL reserved keywords.
https://dev.mysql.com/doc/refman/5.7/en/keywords.html
By adding backtick, you are telling MySQL that the word enclosed is not a MySQL keyword but, rather a database name, table name or field name.
Thus, any conflict is avoided.
DELETE is a reserved keyword in SQL. Without escaping it with the backtick character you will get a syntax error and the query will not work.
Related
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I use below php code to generate a random id number
md5(uniqid(rand(), true)
the type of string it generate is something like this
9a423553ce53c4d7a6199fa9254bfdc5
I use that as an ID in Mysql table then I do a standard select query
SELECT * FROM table WHERE id = 9a423553ce53c4d7a6199fa9254bfdc5
I get this error
Unknown column '9a423553ce53c4d7a6199fa9254bfdc5' in 'where clause'
if I just change the id to a simple number like 1 it works.
Why is this?
Have you tried encapsulating that in quotes? In your query id = 9a423553ce53c4d7a6199fa9254bfdc5 You can compare two columns like id = other_id .. MySQL needs to know how to handle your query.
For clarification, should be: SELECT * FROM table WHERE id = '9a423553ce53c4d7a6199fa9254bfdc5';
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
I have a table with a primary column "MatchId", followed by many other columns.
Unfortunately, I can't seem to get my insert/update query right: Even if I only want to insert an MatchId (Not auto-increment by the way), I get the error Unknown column in 'field list'...
Here is my query:
INSERT INTO `stats` (`MatchId`) VALUES (`123456`);
How do I insert something in this table without getting this error?
You have the wrong types of quotes around the value. Backticks are used around table and column names. To quote a string, use single or double quotes:
INSERT INTO `stats` (`MatchId`) VALUES ('123456');
If it's an integer, you don't need to quote it at all:
INSERT INTO `stats` (`MatchId`) VALUES (123456);
Putting a value in backticks forces it to be treated as a column name, even though it has the syntax of a number. Backticks are the way that MySQL allows you to use column names that have unusual syntax.
Test it in phpmyadmin the unrecognised field is "123456". Change your SQL and wrap the value in single quotes
This question already has answers here:
How do I escape reserved words used as column names? MySQL/Create Table
(4 answers)
Closed 9 years ago.
I'm running this on my website:
$PlaceOrder = " Insert INTO Order VALUES ( '$CustomerID' , '$ItemID' , '$Quantity' , '$Date' ) ";
$result = mysql_query ($PlaceOrder);
if (!$result)
{
die('Invalid query: ' . mysql_error());
}
But when I ever I do I keep getting the following error message:
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 'Order VALUES ( '3' , '1' , '12' , '11/05/2013' )' at line 1
I really have no idea what to do now, I tried specifying the columns but that didn't work either. I am very new to this whole thing and I'm executing this based on whats in the manual.
Let's look at your error message again:
[...] the right syntax to use near'OrderVALUES
The first thing it complains about is Order. That's what you substitute the Table from your question for. So let's assume that's your actual table name.
It's also a reserved word. Enclose reserved words in backticks when used as column or table name identifiers.
Order is Key word Just use another word instead of Order
Are you sure there are only 4 columns and are they in the same order as you have specified in your query?
As you suggested try to add the column names, for example:
$PlaceOrder = "INSERT INTO Table (customer, item, quantity, date) VALUES ('$CustomerID', '$ItemID', '$Quantity', '$Date')";
Does this help you?
First of all, you should always specify the columns since you never know when you will have to add new columns to the table and old queries will start doing messy things if you haven't done that.
Having said that, are you sure those are the correct column data types that match the table's?
Also, make sure the date format is valid, you shoud use Y-m-d. You will find more info here.
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.
I created a table with 3 columns
ID (Primary key), when (some varchar value), Created_date(timestamp)
Will naming a field with "When" creates any issue?
I have been querying like update table set table.when='$when' where ID='1'
Please suggest
it's ok as long as you have to use the tableName along with the column name
update `table`
set `table`.when='$when'
where ID='1'
SQLFiddle Demo
otherwise, wrap it with backticks
update `table`
set `when`='$when'
where ID='1'
SQLFiddle Demo
Other Link:
MySQL Reserved Keywords List
if possible don't use names or identifiers which are on the reserved keyword list to avoid problems.
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 4 years ago.
i try to update simple data to table name "order" but i still get error.
i try to many version query but still same ;
first try :
$result = mysql_query("UPDATE order SET order_status_id=200 WHERE order_id=75") or die(mysql_error());
second try :
$result = mysql_query("UPDATE order SET order_status_id='200' WHERE order_id='75'") or die(mysql_error());
error ;
first try :
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 SET order_id=200 WHERE order_id=75' at line 1
second try :
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 SET order_status_id='200' WHERE order_id='75'' at line 1
Table structure
order_id int(11)
order_status_id int(11)
i try to update others table just to make sure my query correct and all table can update.
*Im using Opencart and my site use https.
Thanks.
order is a reserved word in MySQL. You need to escape it with backticks:
UPDATE `order` SET order_status_id=200 WHERE order_id=75
See MySQL reserved words