【发布时间】:2013-02-20 22:22:35
【问题描述】:
我的更新脚本有一些问题。我绑定了我的值,但它返回 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);
}
错误:
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); -
如果
PDOStatement::execute()返回FALSE,您不应断定您做错了什么,因为它是该函数的有效返回值。它只是意味着语句出现故障,并且语句本身包含错误的信息。只需选择错误信息php.net/manual/en/pdostatement.errorinfo.php 它会告诉您有关失败的更多信息。因此,您应该提供具体的错误信息,而不是只在您的问题中讲述 FALSE(这可能意味着很多)。 -
应该注意上面的 pdo 脚本不是我的工作。在 SO 上找到它 - 找不到线程。问题更新错误
-
这组函数在我看来完全没用。为什么不使用原始 PDO?
-
@YourCommonSense - 所以你说我不需要使用这组函数,而是使用 f.x 中的 PDO。我的单个广告更新函数( update_advert() )?
标签: php mysql pdo sql-update