MySQL query sanity check, for update query - mysql

I have this query
update user_remember_me set
when='2012-07-06 05:44:27',
hash='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
where user = '21';
and I am getting this error
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 'when='2012-07-06 05:44:27', hash='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7' wher' at line 1
Im failing to miss the connection here, I've used simple updates like this everywhere without issue til this, maybe Im getting to tired, but this is gonna drive me nuts til I have an answer

When is a key word in mysql, change the column name
or you can use it as
`when`='2012-07-06 05:44:27'

when is a reserved word in mysql
update user_remember_me set
`when`='2012-07-06 05:44:27',
`hash`='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
where user = '21';
So you must backtick your column

when is a keyword within MySQL. You have to escape it, if you want to use it as a column identifier (as you should with all column identifiers!):
UPDATE user_remember_me
SET
`when`='2012-07-06 05:44:27',
`hash`='c8e9d2c0dd156b5c68d0b048e5daa948e6b8fac7'
WHERE `user` = '21';

Related

SQL ERROR - Error sql syntax

I am getting an error that I dont know how to deal with.
I am running the same code without issue for another column but for this column it refuses to work.
SELECT * FROM Players WHERE Character = 'momo' // This one wont work
SELECT * FROM Players WHERE Class = 'Fighter' // this one works
Character is a VARCHAR and Class is TEXT. I have tried changing Character to TEXT and I still get the same issue. The value 'momo' exists in the table.
ERROR: Couldn't connect to server. SQLSTATE[42000]: Syntax error or access violation: 1064 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 '= ''' at line 1
Edit:
I am editing this incase someone find this and wants to know how it was fixed. User by the name of ueerdo Pointed out that I should use quotations and when I did, it worked. So I started looking into why it happened and I found out the SQL reserves Character for something else so it is something that I can't use unless it is in quotations.
It is best to delimit identifiers to prevent possible collision with reserved words and keywords.
SELECT * FROM `Players` WHERE `Character` = 'momo'

MySQL schedule update syintax wrong?

I'm trying to set a trigger in my MySQL database, so I'm using below query:
CREATE EVENT pwdupdate
ON SCHEDULE EVERY 3 MINUTE
DO
UPDATE 'passwords'
SET 'pass'= SUBSTRING((RAND())
Basically I want it to update the pass field of a table with a random string, but it keeps telling me that there's a syintax error:
1064 - 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 ''passwords' SET 'pass'= SUBSTRING((RAND())' at line 5
Any help ?
EDIT : I removed the single quotes as suggested, but still the error remains
You do not need single quotes for table and column name and its invalid in mysql however you can use backticks ``
UPDATE passwords
SET pass= substring(RAND(),8)

Syntacts error in mysql query

Someone gave me this query to delete community from a database on my server using phpMyAdmin, it worked when he use it so I asked him to send it to me, but I get a error
MySQL said:
Documentation
#1064 - 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 '"SELECT * FROM `connections` WHERE 1" :
delete from connections where communit' at line 1
I did a search on the error but could not figure it out.
"SELECT * FROM `connections` WHERE 1" :
delete from connections where community="XYZ"
You should separate your queries with ; and not :
The error you are receiving as you've shown it is because this line is incorrect:
SELECT * FROM `connections` WHERE 1"
First because it's ending in an unnecessary double quotation mark. MySQL does not use double quotes but single quotes, and even still the other single quote is not there to match it.
The single quotes are also a problem for community="XYZ", this should read: community = 'XYZ'
Secondly, you don't have a condition for your where statement, you must be missing something like:
WHERE columnName = 1;
If you were trying to select everything from connections, you can just remove that where clause all together.
EDIT
In addition, MySQL queries are separated by a semi-colon, not a colon, so MySQL will not realize you are trying two different queries.

SImple MySQL update not working with generic error

I have never had this happened to me before, it is very very strange,
very simple SQL update is not working:
UPDATE table givi_user_sessions set givi_user_clientid='somevalue' where givi_user_id=2;
i tried other variations like:
UPDATE table givi_user_sessions set where givi_user_id=3 where givi_user_id=2
and this too:
UPDATE table `givi_user_sessions` set where `givi_user_id`=3 where `givi_user_id`=2
All those options gave me following error:
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 'table givi_user_sessions set givi_user_clientid='somevalue' where givi_user_id=2' at line 1
I double checked that table exists, and also that column names are correct,
the only thing that I can recall is that i changed table name from user_sessions to givi_user_sessions, but that should not matter at all, unless something got messed in mysql engine, because I definately think that my sql is correct. or maybe i have been working for too long today.
any advices would be appreciated.
You don't need to include the keyword "table" in your query. You can check the syntax of the update query here: http://www.w3schools.com/php/php_mysql_update.asp
It should look like this:
UPDATE givi_user_sessions set givi_user_clientid='somevalue' where givi_user_id=2;

MySQL Syntax with DELETE FROM?

I have this bit of SQL that always returns an error, though I can't find why it is returning the error. I have connected to the database with no errors. I'm running PHP 5.2.17, MySQL 5.5.25a, and Apache 2.4.2.
The SQL:
DELETE FROM mail WHERE to=1
The 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 'to=1' at line 1
TO is a reserved word, you need to use backticks:
DELETE FROM mail WHERE `to` = 1
By adding backticks on the column name escapes in from MySQL Reserved Word
DELETE FROM mail WHERE `to`=1
if the column to is not e.g. INT or DEC you should make it to = "1"