2011-12-09 7 views
0

Iveは、クエリ関数を使用してpdoラッパークラスを作成し、配列を自動的に取得して実行前のステートメントに値をバインドします。PDO最後にバインドされたパラメータ

public function query($query, $sprints = NULL) 
{ 
    // Add query to the last query and benchmark 
    $bench['query'] = $query; 

    // Prepare the statement 
    $this->result = $this->pdo->prepare($query); 

    // Process our sprints and bind parameters 
    if(is_array($sprints)) 
    { 
     // Bind our parameters 
     foreach($sprints as $key => $value) 
     { 
      // if we are using "?'s", then we up the keys +1 
      if(is_int($key)) ++$key; 

      // Bid the param based on its type 
      if(is_int($value)) 
      { 
       // Bind the param 
       $this->result->bindParam($key, $value, \PDO::PARAM_INT); 
       $bench['bound'][$key] = $value; 
      } 
      else 
      { 
       // Bind the param 
       $this->result->bindParam($key, $value, \PDO::PARAM_STR, strlen($value)); 
       $bench['bound'][$key] = $value; 
      } 
     } 
    } 

    // Time, and process our query 
    $start = microtime(true); 
    try { 
     $this->result->execute(); 
    } 
    catch (\PDOException $e) { 
     // show error 
    } 
    $end = microtime(true); 

    // Get our benchmark time 
    $bench['time'] = round($end - $start, 5); 

    // Get our number of rows 
    $this->num_rows = $this->result->rowCount(); 

    // Add the query to the list of queries 
    $this->queries[] = $bench; 

    // Return 
    return $this; 
} 

問題は、挿入時に、すべての?が最後にバインドされたパラメータに置き換えられることです。ここでは、クエリと結果:

INSERT INTO sessions(`token`,`ip_address`,`last_seen`,`user_data`) VALUES (?, ?, ?, ?) 

バウンドのparamsは、次のとおりです。

[bound] => Array ([1] => test1 [2] => 0.0.0.0 [3] => test3 [4] => test4) 

、データベース内の結果は、すべての4列がTEST4で満たされています。誰もがなぜこれをやっているのかという手がかりを持っていますか?あなたの問題ではなく、なぜ何

答えて

2

知らんはそれだけで

$this->result = $this->pdo->prepare($query); 
$this->result->execute($sprints); 
+0

これについては、よく混乱しています。このようにしてswl注入からまだパラメータが消去されていますか? – Wilson212

2

使用bindValue、ないbindParamをしないように。

<?php 
if (count($this->_params) > 0) 
    { 
     foreach ($this->_params as &$param) 
     { 
      $statement->bindValue(":{$param->name}", $param->value, $param->type); 
     } 
    } 
関連する問題