Mysql PDO,查询exec失败

时间:2014-07-18 13:39:13

标签: php mysql pdo

我在升级其中一个应用程序时遇到了奇怪的问题

  • 脚本语言是PHP
  • 数据库是MYSQL
  • 我使用PDO类来处理

我有这个简单的用户更新模块,其中包含4个不同的字段并且工作得很好,今天我决定添加更新用户组的能力,并且查询exec从那时起就失败了。

db中的

- users表包含以下字段:

  • 用户名(varchar 50)
  • 密码(varchar 64)
  • 电子邮件(varchar 128)
  • name(varchar 50)
  • group(int 1)
  • active(int 1)

查询我在更新时正在执行,在upgreade之前是这样的:

UPDATE users SET username = ?, email = ?, name = ?, active = ? WHERE id = 1
Array
(
    [username] => john
    [email] => john.doe@gmail.com
    [name] => John Doe
    [active] => 1
)

它工作得很好,现在我正在执行的查询是这样的:

UPDATE users SET username = ?, email = ?, name = ?, group = ?, active = ? WHERE id = 1
Array
(
    [username] => john
    [email] => john.doe@gmail.com
    [name] => John Doe
    [group] => 2
    [active] => 1
)

这个不起作用,但如果我检查了errorinfo,它会返回数组:

Array
(
    [0] => 00000
    [0] => 
    [0] => 
)

所以我试图只用这种方式更新没有其他字段的组:

UPDATE users SET group = ? WHERE id = 1
Array
(
    [group] => 2
)

也失败了,这是奇怪的,如果我把它复制到phpmyadmin sql runer它正在被正确执行。

执行exec的代码是:

if($this->_query = $this->_pdo->prepare($sql)) {
    if(count($params)) {
        $x = 1;
        foreach($params as $param) {
            $this->_query->bindValue($x, $param);
            $x++;
        }
    }

    if($this->_query->execute()) {
        $this->_insertid = $this->_pdo->lastInsertId();
        $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
        $this->_count = $this->_query->rowCount();
    } else {
        $this->_error = true;
    }
}

除非我尝试更新群组,否则一切都在工作,只有这样才会失败,我不明白也许有人会这样做,请大家提出所有建议:)

BTW也许值得一提的是,当我INSERT而不是UPDATE正确执行时,只有更新有一些问题

1 个答案:

答案 0 :(得分:4)

群组是MySQL中的保留字 http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

不知道您的代码中是否还有其他错误,但请尝试在[保留]字词周围使用``

`group`