使用mysql PDO查询REPLACE时出错

时间:2014-09-18 08:55:11

标签: replace pdo

我尝试用pdo执行以下mysql查询

REPLACE INTO session SET id = :id, user_id = :user_id, data = :data, timestamp = :timestamp

我收到以下错误:

[18-Sep-2014 11:48:10] Exception Message: Unhandled Exception.
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 '?, data = ?, timestamp = ?' at line 1.

您可以在日志中找到错误。:

Query:REPLACE INTO session SET id = :id, user_id = :user_id, data = :data, timestamp = :timestamp

PARAMS:

Array
(
    [id] => sv9o264ciicsfd8porp1v0gl46
    [user_id] => 0
    [data] => version|s:8:"computer";linkedin|a:1:{s:5:"state";s:7:"Q7HXzKo";}github|a:1:{s:5:"state";s:7:"Q7HXzKo";}
    [timestamp] => 1411030090
)

我的表会话结构是:

CREATE TABLE IF NOT EXISTS `session` (
  `id` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '',
  `user_id` mediumint(10) NOT NULL,
  `data` text COLLATE utf8_unicode_ci NOT NULL,
  `timestamp` int(40) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `session` (`id`),
  UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

当我使用phpmyadmin执行它时工作正常 你能帮我理解是什么问题吗?

2 个答案:

答案 0 :(得分:0)

你忘记了一个等号。 更改查询的这一部分:

user_id :user_id

为:

user_id = :user_id

我还认为在您的某个列中使用保留关键字timestamp可能会出现问题,请尝试将其更改为tstamp。您也可以尝试在查询中引用该名称。

REPLACE INTO session SET `id` = :id, `user_id` = :user_id, `data` = :data, `timestamp` = :timestamp

答案 1 :(得分:0)

我发现它,非常愚蠢的错误:我应该从参数中分离出来:

我改变了这一点 :user_id,

于: :user_id ,