2016-12-15 3 views
-1

준비 문을 사용하여 개체 변수에 인수로 전달 된 클래스 개체 속성을 사용하여 자리 표시 자 값을 설정하려고합니다. __construct 함수. 그러나 나는 솔기가 자리 표시 자 값에 대해 하나의 매개 변수 만 가질 때 2 개의 매개 변수에 대한 필요성을 지정하는 오류가 발생합니다.PHP - PDO Prepared Statement, "경고 : PDOStatement :: bindParam()에 2 개 이상의 매개 변수가 필요합니다."

CODE :

<?php include ('connection.inc.php'); 

class Team { 

    // Remember to switch back to private! 
    private $database; 
    public $statement = 'SELECT * FROM members ORDER BY :full_name'; 
    public $order; 
    public $query; 

    private $result;  

    public function __construct($database, $order) { 
     $this->database = $database; 
     $this->order = $order; 
     $this->query = $this->database->prepare($this->statement); 
     $this->query->bindParam(array('full_name', $this->order)); 
     $this->query->execute();     
    } 

    public function getMember() {   
     $this->result = $this->query->fetch(PDO::FETCH_ASSOC); 
     return $this->result;       
    } 
    public function id() { 
     echo $this->result['id']; 
    } 

    public function fullName() { 
     echo $this->result['full_name']; 
    } 
    public function alias() { 
     echo $this->result['alias']; 
    } 
    public function abilities() { 
     echo $this->result['abilities']; 
    }  

}; 

$test = new Team($database, 'full_name'); 

?> 

ERRORS :

경고 : PDOStatement가 :: bindParam()가 기대 (1)

치명적인 오류에 주어진 적어도 2 개 개의 파라미터 : 'SQLSTATE [HY093] : 잘못된 매개 변수 번호 : 매개 변수 없음'메시지와 함께 'PDOException'오류가 잡히지 않았습니다. 이 오류를 제거

$this->query->bindParam(':full_name', $this->order)); 

: ERS는 내가 내 bindParam() 문을 변경,

솔루션 '@Daerik에

감사를 구속했다.

답변

1

PDOStatement::bindParam (mixed $parameter , mixed &$variable )

$parameter : 매개 변수 식별자. 명명 된 자리 표시자를 사용하여 준비된 명령문의 경우 name : name 형식의 매개 변수 이름이됩니다. 물음표 자리 표시자를 사용하여 준비된 문에 대해 이것은 매개 변수의 1- 색인 위치가됩니다.

$variable : SQL 문 매개 변수에 바인드 할 PHP 변수의 이름.

당신은 사용할 수 있습니다 : 자세한 내용은

$this->query->bindParam(':full_name', $this->order); 

PDOStatement::bindParam를 참조하십시오. 당신은 매개 변수 이름의 :을 포함해야하고 ':key' => $value쌍 전달해야합니다

$this->query->execute(array(':full_name' => $this->order)); 

참고 :

+0

감사합니다, 모두 오류를 제거 :

또는 단지 하나

는, 배열을 통과하지 못한, bindParam()는 두 개의 인수가 필요합니다. 그러나'gettMember()'메소드를 사용하여 sql 질의를 반복 할 때'ORDER BY ': full_name'문장을 무시하고'id' 컬럼을 사용하여 엔트리를 정렬합니다. 준비된 성명서에 잘못이 있습니까? –

+1

@ FrederickM.Rogers 당신은'ORDER BY' 문에서 컬럼을 사용하고자 할 것입니다. 문자열을 할당하면 모두를 동일한 값으로 비교하고 인덱스를 기본값으로 비교합니다. 자세한 내용은 http://dev.mysql.com/doc/refman/5.7/en/order-by-optimization.html을 참조하십시오. – Daerik

+0

읽어 주시고 필요한 조정을 해 주셔서 감사합니다! –

1

은 사용, 여러 매개 변수를 전달하는 bindParam()를 사용하지 마십시오.

$this->query->bindParam(':full_name', $this->order); 
+1

'PDOStatement :: execute'을 사용할 때':'를 포함 할 필요가 없습니다. – Daerik

+1

@Daerik : 결코 시도하지 않았고 모든 수동 예제는': parameter'를 보여줍니다. 또한 input_parameters의 키는 SQL._에서 선언 된 키와 일치해야하지만 가능할 수도 있습니다. – AbraCadaver

+1

어떤 식별자가 자리 표시 자인지 나타내려면 콜론이 SQL 문에 필요합니다. 'execute()'호출에서 콜론은 선택 사항입니다. 문서에 명시되어 있지만, 구현을 잘하면 충분히 빠져 나가면 무슨 뜻인지 알 수 있습니다. – Daerik