问题
I am using CodeIgniter and I've noticed that one of our fields is named "from" because the table is for saving emails. So someone used "from" which is a reserved word from database.
I've used
$this->db->query("INSERT INTO email_setting (client_id, [from], created_by) VALUES (9251 , 'mjmsample@gmail.com', 1)")
I've also tried these codes
$this->db->query("INSERT INTO email_setting e (e.client_id, e.from, e.created_by) VALUES (9251 , 'mjmsample@gmail.com', 1)")
$this->db->query("INSERT INTO email_setting ('client_id', 'from', 'created_by') VALUES (9251 , 'mjmsample@gmail.com', 1)")
All of these but no luck, is there any workaround this?
回答1:
I don't understand why people never use backticks!
INSERT INTO `email_setting` `e` (`e`.`client_id`, `e`.`from`, `e`.`created_by`)
回答2:
Use `` backtick symbol to use reserved keyword in mysql.It is a good practice to add backtick to all fields.
USE `from` with backtick instead of 'from' with single quotes.
$this->db->query("INSERT INTO email_setting (`client_id`, `from`, `created_by`) VALUES (9251 , 'mjmsample@gmail.com', 1)"
回答3:
You need to use back ticks for MySQL reserved keywords:
$this->db->query("INSERT INTO `email_setting` (`client_id`, `from`, `created_by`) VALUES (9251 , 'mjmsample@gmail.com', 1)")
It's a good habit to use back ticks for all MySQL-related names such as table names and columns.
回答4:
You need to use backtick(`) quotes instead of '
来源:https://stackoverflow.com/questions/37000314/how-do-i-insert-a-record-in-sql-with-reserved-word-from