2015-01-19 3 views
0

나는 고급 검색을하려고하고, 나는이 작업을했다하지만 난 좋지 않았다 변수를 얻을 각각 전체 쿼리를 만들고 있었다하지만 내가 구축을 위해 노력하고 지금 PDO 고급 검색 쿼리 작성 문제

근무 url 변수가 비어 있는지 여부에 따라 쿼리가 수행됩니다. 그러나 이것이 왜 작동하지 않는지 우리는 짐작할 수 없습니다. 쿼리를 올바르게 테스트하지 못했지만 쿼리가 제대로 작동합니까?

함수

public function search($man, $mod, $min, $max) 
    { 
     $this->currentLocation(); 
     $bind = ''; 

     $query .= 'SELECT listed_watches.*, watch.*, watch_images.* FROM listed_watches'; 

     $query .= 'LEFT JOIN watch ON watch.Id = listed_watches.watchID'; 

     $query .= 'LEFT JOIN watch_images ON listed_watches.url = watch_images.url'; 

     $query .= 'WHERE listed_watches.sellto REGEXP :search'; 

     if(!empty($man)){ 
      $query .= 'AND listed_watches.manufacture = :man'; 
      $bind .= "$smt->bindValue(':man', $man);"; 
     } 

     if(!empty($mod)){ 
      $query .= 'AND listed_watches.model = :mod'; 
      $bind .= "$smt->bindValue(':mod', $mod);"; 
     } 

     if(!empty($min)){ 
      $query .= 'AND listed_watches.minPrice > :min'; 
      $bind .= "$smt->bindValue(':min', $min);"; 
     } 

     if(!empty($max)){ 
      $query .= 'AND listed_watches.maxPrice < :max'; 
      $bind .= "$smt->bindValue(':max', $max);"; 
     } 

     $query .= 'GROUP BY listed_watches.id'; 

     $smt = $this->pdo->prepare(''.$query.''); 

     $smt->bindValue(':search', "^[".$this->userLocation."]"); 
     $bind; 
     return $smt->fetchAll(); 

    } 

내가 검색 및 입력 선택 달러 (A $) 남자와 $ 월 내가

Undefined variable: smt 
Trying to get property of non-object in 

이 오류이 줄

$bind .= "$smt->bindValue(':mod', $mod);"; 
에 심판되어이 오류를 얻을 때

답변

0

먼저 $ smt 개체를 만든 다음 메서드를 사용할 수 있습니다.

if(!empty($man)){ 
     $query .= 'AND listed_watches.manufacture = :man'; 
    } 
    if(!empty($mod)){ 
     $query .= 'AND listed_watches.model = :mod'; 
    } 

    // ... 

    $query .= 'GROUP BY listed_watches.id'; 

    $smt = $this->pdo->prepare(''.$query.''); 

    $smt->bindValue(':search', "^[".$this->userLocation."]"); 

    if(!empty($man)){ 
     $smt->bindValue(':man', $man); 
    } 
    if(!empty($mod)){ 
     $smt->bindValue(':mod', $mod); 
    } 
+0

덕분에,하지만 지금은 유효하지 않은 매개 변수 번호를 받고 메신저 : 매개 변수가 정의되지 않았습니다 ->이 당신이 올바르게 할 경우이 같은 번호가있는 경우, 확인이 불가능하다 –

+0

) (실행 가리키는 : params_in_sql 및 bind params, "if"에 "echo"를 추가하십시오. –