我在更新腳本中遇到了一些問題。我綁定了我的值,但它返回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