2010-03-18 2 views
1

저는 PDO를 배우기 위해 PDO CRUD를 조금하려고합니다. bindParam에 대한 질문이 있습니다. 여기 내 업데이트 방법은 지금의 내 "자기 :: $의 condition_array"에정말 bindParam이 필요합니까?

public static function update($conditions = array(), $data = array(), $table = '') 
{ 
    self::instance(); 

    // Late static bindings (PHP 5.3) 
    $table = ($table === '') ? self::table() : $table; 

    // Check which data array we want to use 
    $values = (empty($data)) ? self::$_fields : $data; 

    $sql  = "UPDATE $table SET "; 
    foreach ($values as $f => $v) 
    { 
     $sql .= "$f = ?, "; 
    } 

    // let's build the conditions 
    self::build_conditions($conditions); 

    // fix our WHERE, AND, OR, LIKE conditions 
    $extra = self::$condition_string; 

    // querystring 
    $sql = rtrim($sql, ', ') . $extra; 

    // let's merge the arrays into on 
    $v_val = array_values($values); 
    $c_val = array_values($conditions); 
    $array = array_merge($v_val, self::$condition_array); 

    $stmt = self::$db->prepare($sql); 
    return $stmt->execute($array); 
} 

을 나는 모든 권리 값을 얻을?. SO 쿼리는 다음과 같습니다

UPDATE table SET this = ?, another = ? WHERE title = ? AND time = ? 

당신은 내가 직접 실행 ($ 배열) 방법에 내가 올바른 순서 ($ 배열)에 올바른 값을 전달하는 대신 bindParams를 사용 해달라고 볼 수 있습니다. 이것은 매력처럼 작동하지만 여기서는 bindParam을 사용하지 않는 것이 안전할까요?

그렇다면 어떻게해야합니까?

답변

2

예, 그것은 안전 스웨덴

토비아스에서

감사합니다. bindParam()은 변수와 매개 변수를 연결하며 execute()이 호출 될 때 변수 값을 사용하려는 경우에 사용합니다. 그렇지 않으면 당신이하고있는 일이 잘됩니다.

PHP 문서 도구 PDO bindParam()