-1

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?

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
MJ Convento
  • 213
  • 3
  • 8

4 Answers4

4

I don't understand why people never use backticks!

INSERT INTO `email_setting` `e` (`e`.`client_id`, `e`.`from`, `e`.`created_by`)
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1
 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)"
Sanooj T
  • 1,317
  • 1
  • 14
  • 25
0

You need to use backtick(`) quotes instead of '

beerwin
  • 9,813
  • 6
  • 42
  • 57
Valery K.
  • 147
  • 1
  • 5
0

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.

Panda
  • 6,955
  • 6
  • 40
  • 55