2013-03-06 43 views
-1

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

+2

'$ DB->的setAttribute(PDO :: ATTR_ERRMODE,PDO :: ERRMODE_EXCEPTION);' – 2013-03-06 08:16:41

+0

如果'PDOStatement對象::執行()'返回'FALSE',你不應該得出這樣的結論:你做錯了什麼,因爲它是該函數的有效返回值。這隻意味着該陳述出現了失敗,並且陳述本身包含錯誤信息。只需選擇錯誤信息http://www.php.net/manual/en/pdostatement.errorinfo.php它會告訴你更多關於失敗的信息。因此,不要僅在您的問題中告知FALSE(這可能意味着很多),您應該提供具體的錯誤信息。 – hakre 2013-03-06 08:25:57

+0

應該注意,上面的pdo腳本不是我的工作。在這裏找到它 - 找不到線程。 問題更新錯誤 – 2013-03-06 09:13:47

回答

0

剛剛擺脫這種綁定功能和沒事

$sql = 'UPDATE '. $config->db_prefix .'_adverts SET 
     ad_type = ?, title = ?, text = ?, price = ?, category = ?, condition = ? 
     WHERE aid = ?'; 
$db->query($sql); 
$db->execute(array($ad_type, $title, $text, $price, $category, $condition, $aid)); 
2

您只能對準備好的語句使用綁定函數。​​用於在事先已經知道查詢的所有值/變量的情況下生成查詢。

+0

他仍然在佔位符中使用'PDO :: query'。對我來說沒有意義。 – silkfire 2013-03-06 08:24:41

+1

可能不命名'prepare'方法'query'的一個原因。我猜OP試圖創建一個流暢的界面。 – hakre 2013-03-06 08:29:11

+0

他沒有使用'PDO :: query()',這是他自己的代碼,它返回一個語句。 – 2013-03-06 08:29:18

-1

從你的問題不能具體說錯誤的位置。因此,我建議你先添加更多錯誤檢查,例如與bindvalue(),你不檢查返回值:

$this->stmt->bindValue($pos, $value, $type); 

而是拋出一個異常,如果這個失敗:

$bind = $this->stmt->bindValue($pos, $value, $type); 
if (!$bind) { 
    throw new Exception(sprintf("Unable to bind parameter %s with a value of type #%d", $pos, $type)); 
} 

這會防止你有問題,結合一個值,但它得到了被忽視。

與執行類似。如果你想提供信息失敗的原因,你需要讀出錯誤信息並拋出異常。

相關問題