db_merge() PDO error The Error:

 PDOException: 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 'key, value) VALUES ('1', '1', 'mydata', '35')' at line 1: INSERT INTO {mytable} (uid, tid, key, value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3); Array ( [:db_insert_placeholder_0] =&gt; 1 [:db_insert_placeholder_1] =&gt; 1 [:db_insert_placeholder_2] =&gt; mydata [:db_insert_placeholder_3] =&gt; 35 ) in MyClass-&gt;save_myData() (line 145 of /path/to/my/stuff/my.class.php).


My Code:

db_merge( 'mytable' )
    -&gt;key( array( 'uid' =&gt; $this-&gt;user-&gt;uid, 'tid' =&gt; $data['tid'] ) )
    -&gt;insertfields(array(
           'uid' =&gt; $this-&gt;user-&gt;uid,
            'tid' =&gt; $data['tid'],
            'key'      =&gt; $key,
            'value'    =&gt; $value
    ) )
     -&gt;updateFields( array( 'value' =&gt; $value ) )
     -&gt;execute();


I've tried this with just the basic ->fields() but my latest iteration here involves specifiying the inserted fields and updated fields separately (just ran across this while googling and thought it might help.  Same error though.

Originally I was trying to use

db_query( "REPLACE INTO goal_data(uid,tid,key,value) VALUES( :uid,:tid,:key,:value)", array(
                ':uid'      =&gt; $this-&gt;user-&gt;uid,
                ':tid'      =&gt; $data['tid'],
                ':key'      =&gt; $key,
                ':value'    =&gt; $value
            ) );


It too was throwing an error saying that there were in invalid number of placeholders or something similar.  There wasn't. The error was basically:

PDOException: 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


Any thoughts on what I'm doing wrong?  I don't get it, trying everything here and just keep getting errors. 

Here's my table structure (very simple).

CREATE TABLE IF NOT EXISTS `mytable` (
  `uid` int(11) NOT NULL,
  `tid` int(11) NOT NULL,
  `key` varchar(255) NOT NULL,
  `value` longtext NOT NULL,
  PRIMARY KEY (`uid`,`tid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The syntax error comes from key being a MySQL reserved keyword.
The best solution is to not use these keywords as identifiers.
 Otherwise the workaround is to enclose them in backticks.

It may work like this:

  [...]
  -&gt;insertFields(array(
           'uid' =&gt; $this-&gt;user-&gt;uid,
            'tid' =&gt; $data['tid'],
            '`key`'    =&gt; $key,
            'value'    =&gt; $value
    ) )
   [...]


However this fails at cross-database compatibility since the backtick is a MySQL-only syntax. Better rename the column if possible.