2013-06-23 43 views
1

您好,我有以下功能和以下類。問題是它只綁定了陣列中的一個鍵,而不是所有鍵,我沒有理由爲什麼。bindParam在循環內不能正常工作

public function addData(Array $data) 
{ 
    $this->beginTransaction(); 
    $this->query('INSERT INTO test (name, date) VALUES (:invoiceName, NOW())'); 
    $this->bind(':invoiceName', uniqid()); 
    $this->execute(); 
    $this->query('INSERT INTO test_table (testId, username, type) VALUES (:invoiceId, :username, :type,)'); 
    $this->bind(':invoiceId', $this->lastInsertId()); 
    foreach($data as $key => $value) 
    { 
     $this->bind(':username', $value['name']); 
     $this->bind(':type', $value['type']); 
    } 
    $this->execute(); 
    $this->endTransaction(); 
} 

和我正在使用它進行數據庫連接的類。

<?php 
use PDO; 
use PDOException; 
class Database 
{ 
    private 
     $pdoOptions = array 
      (
       PDO::ATTR_PERSISTENT => true, 
       PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, 
       PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8', 
       PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, 
       PDO::ATTR_PERSISTENT => true, 
      ); 
    private 
     $connectionConfig = array 
      (
       'dsn' => 'mysql:dbname=Test;host=127.0.0.1', 
       'username' => 'root', 
       'password' => '' 
      ); 
    private $dbh; 
    private $stmt; 

    public function setOptions($pdoOptions = array()) 
    { 
     $this->pdoOptions = array_merge($this->pdoOptions, $pdoOptions); 
    } 

    public function getOptions() 
    { 
     return $this->pdoOptions; 
    } 

    public function setConnectionConfig($connectionArray = array()) 
    { 
     $this->connectionConfig = array_merge($this->connectionConfig, $connectionArray); 
    } 

    public function getConnectionConfig() 
    { 
     return $this->connectionConfig; 
    } 

    public function createInstance() 
    { 
     if (!extension_loaded('PDO')) 
      throw new PDOException(__CLASS__ . ': The PDO extension is required for this class'); 
     try { 
      $this->dbh = new PDO($this->connectionConfig['dsn'], $this->connectionConfig['username'], $this->connectionConfig['password'], $this->pdoOptions); 
     } catch (PDOException $e) { 
      $this->error = $e->getMessage(); 
     } 
    } 

    public function query($query) 
    { 
     $this->stmt = $this->dbh->prepare($query); 
    } 

    public function bind($param, $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_INT; 
        break; 
       case is_null($value): 
        $type = PDO::PARAM_NULL; 
        break; 
       default: 
        $type = PDO::PARAM_STR; 
      } 
     } 

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

    public function execute() 
    { 
     $this->stmt->execute(); 
    } 

    public function resultSet($type = PDO::FETCH_ASSOC) 
    { 
     $this->execute(); 
     return $this->stmt->fetchAll($type); 
    } 

    public function singleResult($type = PDO::FETCH_ASSOC) 
    { 
     $this->execute(); 
     return $this->stmt->fetch($type); 
    } 

    public function rowCount() 
    { 
     return $this->stmt->rowCount(); 
    } 

    public function lastInsertId() 
    { 
     return $this->dbh->lastInsertId(); 
    } 

    public function beginTransaction() 
    { 
     $this->dbh->beginTransaction(); 
    } 

    public function endTransaction() 
    { 
     $this->dbh->commit(); 
    } 

    public function cancelTransaction() 
    { 
     $this->dbh->rollBack(); 
    } 

    public function debugDumpParams() 
    { 
     return $this->stmt->debugDumpParams(); 
    } 
} 
+0

它不綁定的所有參數,因爲你只有1查詢插槽。它會綁定$ data數組中的最後一個。 – user4035

+3

不應該在循環內執行嗎? – Musa

+0

@Musa是的,我發佈後在這裏測試了工作:-d – Bogdan

回答

1

你需要把foreach循環這樣的內部執行:

foreach($data as $key => $value) 
{ 
    $this->bind(':username', $value['name']); 
    $this->bind(':type', $value['type']); 
    $this->execute(); 
}