我在模型中有查询功能:
public function query($qry, $params = array()) {
try {
$pdo = $this->prepare($qry);
$pdo->execute($params);
return $pdo->fetchAll(PDO::FETCH_OBJ);
}
catch (PDOException $e) {
return '<div class="error message">' . $e->getMessage() . '</div>';
}
}
我希望使用以下代码将一些数据插入表中:
$email = $_POST['email'];
$key = $this->key_generator();
$password = sha1($_POST['password']);
$qry = "
INSERT INTO users
(email, password, key)
VALUES (:email, :password, :key)
";
$params = array(
':email' => $email,
':password' => $password,
':key' => $key
);
$result = $this->query($qry, $params);
注意:这只是测试代码...
我抓住了异常:
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) VALUES
我无法弄清楚key)
附近的语法错误在哪里。
答案 0 :(得分:2)
您应该使用反引号(`)符号来包含每个列名,因为它可能包含查询中的mysql关键字;密码和密钥是mysql的关键字。