Single quotes should only be used in values. So they are ok in VALUES ('blabla')
. Already in table names, columns and aliases, or you don’t use anything, or you use backticks `
. These backticks are only required if the name of your table, column, or alias matches some reserved Mysql term. So in your second query, this part:
INSERT INTO '_user' ('firstname')
has syntax errors. The simplest correct query would look like this (already in a string):
"INSERT INTO _user (firstname) VALUES ('blabla')"
Example of use that requires backtick, if you had an INT type column called "order" (which is reserved word):
"INSERT INTO _user (firstname, `order`) VALUES ('blabla', 1)"
It doesn’t hurt to remember that it is always good to avoid using reserved words such as database name, table, column, alias, index, function, precedent etc.
Add code and error message.
– rray
No error, query just doesn’t work.
– Thiago
You are using Mysql?
– mgibsonbr
You must use backtick
\`` em nomes de campos ou tabelas, com prepared statements não é necessário usar aspas simples nos valores basta fazer o
bindValue()ou passar todos os valores no
execute()`.– rray
Yes, Mysql with PDO
– Thiago
The funny thing is that I don’t have this problem in select, this is only in Insert?
– Thiago
No, the rule is the same in any SQL command.
– bfavaretto