2015-07-05 29 views
0

例如,我在我的數據庫中有幾個表格,例如用戶,產品等。在每個表格中,我至少有一個關聯的類以及一些方法,例如addUser,updateUserName,updateUserPassword等。對於每一個方法,我需要準備的SQL使用PDO,它看起來像這樣的時候:如何使使用PDO的數據庫查詢更高效?

$sql = "INSERT INTO `user` 
(`id`,`username`,`password`,`log`) 
VALUES 
(:id, :username, :password, :log)"; 

然後我的值存儲在一個數組是這樣的:

$array = array('id'=>$id, 'username'=>$username, 'password'=>$password, 'log'=>$log); 

然後我用的是PDO的事:

$pdo = new PDO($dsn, $user, $password); 
$mysql = $pdo->prepare($sql); 
$mysql->execute($array); 

所以看來,對於User類中的所有不同的方法,我需要做這個「準備」的事情。這不是太乏味嗎?有沒有更有效的方法來做到這一點,尤其是考慮到存在一個有許多列的表格的情況下,我將值存儲在一個數組中的部分,在這種情況下,我最終會得到一個很長的準備句子?

+0

IM困惑,爲什麼你會需要這個*每*方法,而不只是一次 –

+0

我不太明白你所說的「僅僅意味着什麼一。」如果不同的方法需要準備不同的數組,並且不一定需要在數組中準備所有的函數參數,例如在使用LIMIT $ offset,$ page_size進行查詢時,不需要準備這兩個參數。那麼我該如何準備這些值呢? – Dainy

+0

你是否在OOP裝備好,如果是的話我覺得單個DB類可以幫助 –

回答

-1

因爲你自己是INSERT和UPDATE嘗試這些

 //to query the database with prepared statements 
    public function query ($sql, $parameters = array()) { 

     //setting error to false to prevent interferance from previous failed queries 
     $this->_error = false; 

     //prepare SQL statement 
     if ($this->_query = $this->_pdo->prepare ($sql)) { 

      //checking to see whether any parameters were submitted along 
      if (count($parameters)) { 

       //setting the initial position for the binding values 
       $position = 1; 

       //getting the individual parameters and binding them with their respective fields 
       foreach ($parameters as $param) { 
        $this->_query->bindValue ($position, $param); 
        $position++; 
       } 
      } 
     } 

     //executing the sql 
     if ($this->_query->execute()) { 
      //getting the number of rows returned 
      $this->_count = $this->_query->rowCount(); 

      //keeping the results returned 
      $this->_results = $this->_query->fetchAll (PDO::FETCH_OBJ); 
     } else { 
      $this->_error = true; 
     } 
     //returning all values of $this 
     return $this; 
    } 


     //to insert data into a prescribed table 
    public function insert ($table, $parameters = array()) { 

     //checking if the $fields are not empty 
     if (count($parameters)) { 

      //making the keys of the array fields 
      $fields = array_keys ($parameters); 

      //creating the to-bind-values in the form (?, ?, ...) 
      $values = ''; 
      $x = 1; 

      foreach ($parameters as $field => $value) { 

       //$value is different from $values 
       $values .= '?'; 

       if ($x < count($parameters)) { 
        $values .= ', '; 
        $x++; 
       } 
      } 
      //generating $sql 
      $sql = "INSERT INTO `{$table}` (`".implode ('`, `', $fields)."`) VALUES ({$values})"; 

      //executing the sql 
      if (!$this->query($sql, $parameters)->error()) { 
       return true; 
      } 
     } 
     return false; 
    } 

    //to update data in a prescribed table 
    public function update ($table, $id = null, $parameters = array()) { 

     //checking that $parameters is not an empty array 
     if (count($parameters)) { 
      $set = ''; 
      $x = 1; 

      foreach ($parameters as $field => $value) { 
       $set .= "`{$field}` = ?"; 

       if ($x < count($parameters)) { 
        $set .= ', '; 
        $x++; 
       } 
      } 

      if ($id) { 
       //generating query 
       $sql = "UPDATE `{$table}` SET {$set} WHERE `id` = {$id}"; 
      } else { 
       $sql = "UPDATE `{$table}` SET {$set} WHERE 1"; 
      } 

      //executing the query 
      if (!$this->query($sql, $parameters)->error()) { 
       return true; 
      } 
     } 
     return false; 
    } 
+0

他正在使用準備好的語句,你的不是 –

+0

你能否再次閱讀答案 –

+0

它的哈希投票人如果你真的沒有花時間閱讀他/她的答案,那根本就不是很酷 –