我在更新脚本中遇到了一些问题。我绑定了我的值,但它返回false,我看不到我做错了什么。pdo绑定在更新中返回false
我正在此:
$row = $db->query('
UPDATE '. $config->db_prefix .'_adverts
SET ad_type = ?,
title = ?,
text = ?,
price = ?,
category = ?,
condition = ?
WHERE aid = ?')
->bind(1, $ad_type)
->bind(2, $title)
->bind(3, $text)
->bind(4, $price)
->bind(5, $category)
->bind(6, $condition)
->bind(7, $aid)->execute();
}
绑定功能是这样的:
public function bind($pos, $value, $type = null) {
if(is_null($type)) {
switch(true) {
case is_int($value):
$type = PDO::PARAM_INT;
break;
case is_bool($value):
$type = PDO::PARAM_BOOL;
break;
case is_null($value):
$type = PDO::PARAM_NULL;
break;
default:
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($pos, $value, $type);
return $this;
}
一个var_dump($this)
给我:
object(DB)#1 (2) { ["dbh":protected]=> object(PDO)#2 (0) { } ["stmt":protected]=> object(PDOStatement)#15 (1) { ["queryString"]=> string(211) " UPDATE rno_adverts SET ad_type = ?, title = ?, text = ?, price = ?, category = ?, condition = ? WHERE aid = ?" } }
,但我看不到什么是错的。
编辑:
查询功能是这样的:
public function query($query) {
$this->stmt = $this->dbh->prepare($query);
return $this;
}
和执行是这样的:
public function execute($var = null) {
return $this->stmt->execute($var);
}
ERROR:
Uncaught exception 'PDOException' with message '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 'condition = 3 WHERE aid = 1'
查询的输出:
UPDATE rno_adverts SET ad_type = 3, title = "Gul bil", text = "En flot gul bil med hvide striber", price = 500, category = 4, condition = 3 WHERE aid = 1
我在这个查询中失明了,所以我看不出是什么问题。如果我删除类别和条件,它的工作没有问题。数据库中的这两个字段都是INT NOT NULL。
'$ DB->的setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);' – 2013-03-06 08:16:41
如果'PDOStatement对象::执行()'返回'FALSE',你不应该得出这样的结论:你做错了什么,因为它是该函数的有效返回值。这只意味着该陈述出现了失败,并且陈述本身包含错误信息。只需选择错误信息http://www.php.net/manual/en/pdostatement.errorinfo.php它会告诉你更多关于失败的信息。因此,不要仅在您的问题中告知FALSE(这可能意味着很多),您应该提供具体的错误信息。 – hakre 2013-03-06 08:25:57
应该注意,上面的pdo脚本不是我的工作。在这里找到它 - 找不到线程。 问题更新错误 – 2013-03-06 09:13:47