2012-02-02 3 views
0

쿼리 결과에 문제가 있습니다. getSales() 함수는 처음 호출 할 때 훌륭하게 작동합니다. 다시 호출 될 때 쿼리는 결과를 생성하지 않습니다.PDO - 쿼리 결과가 반환되지 않습니다.

abstract class Reporting { 
     protected function connect() { 
      try { 
       $this->dbh = PDOConnection::getInstance(); 

       if (!$this->dbh instanceof PDO) { 
        throw new CustomException('Unable to connect to database'); 
       } 
      } 
      catch (CustomException $e) { 
       echo $e; 
      } 
     } 
} 
class TenMinuteSales extends Reporting { 

     protected $date; 

     public function __construct($date) { 
      $this->date = new DateTime($date); 
      $this->date = $this->date->format('Y-m-d'); 
     } 

     public function beginReport() { 
      parent::connect(); 
     } 

     public function getSales($meridiem, $date) { 
      try { 
       $statement = "SELECT directory.location, IFNULL(sales.daily_sales,0.00) AS sales, IFNULL(sales.cover_counts,0) AS covers 
           FROM t_directory directory 
           LEFT JOIN v_sales_all sales 
           ON sales.site_id = directory.site_id 
           AND sales.business_date = :date 
           AND sales.meridiem = :meridiem 
           ORDER BY directory.site_id ASC 
           LIMIT :totalLocations"; 

       $sth = $this->dbh->prepare($statement); 
       $sth->bindParam(':date', $date, PDO::PARAM_STR); 
       $sth->bindParam(':meridiem', $meridiem, PDO::PARAM_STR); 
       $sth->bindParam(':totalLocations', $this->totalLocations, PDO::PARAM_INT); 
       $sth->execute(); 

       switch ($meridiem) { 
        case 'AM': 
         $this->amSales = $sth->fetchAll(PDO::FETCH_ASSOC); 
         return $this->amSales; 
        case 'PM': 
         $this->pmSales = $sth->fetchAll(PDO::FETCH_ASSOC); 
         return $this->pmSales; 
       } 
      } 
      catch (CustomException $e) { 
       echo $e; 
      } 
     } 

$tms = new TenMinuteSales($date); 
$tms->beginReport(); 
$amSales = $tms->getSales('AM', $date); 
$pmSales = $tms->getSales('PM', $date); 

내가 AM 또는 PM 판매 번호를 getSales()를 호출, 함수가 성공적으로 데이터를 반환 : 여기에 코드의 작은 덩어리이다. 두 번째 호출 할 때 함수는 데이터를 반환하지 않습니다. 그 라인을 따라 결과 나 무언가를 자유롭게해야하는지 잘 모르겠습니다. unset($sth)$sth->closeCursor()을 시도했지만 어느 것도 내 문제를 해결하지 못했습니다. 내 문제를 해결하는 데 큰 도움이 될 것입니다. 자세한 내용이 필요한 경우 알려 주시기 바랍니다. 이 같이

+0

'$ this-> totalLocations'은 정의되지 않았으며 아마도 NULL이 맞습니까? – Wrikken

+0

나는 그것이 설정된 코드를 포함하지 않았습니다. 이 질문의 목적을 위해. '$ this-> totalLocations' = 20 – Brett

+0

문제와 관련이 없지만 try/catch를 악용하는 것 같습니다. getSales()에서의 catch는 결코 일어나지 않을 것입니다. 왜냐하면 아무 것도 그 레벨에 던지지 않기 때문입니다. – Kenaniah

답변

1

는 나중에이 변수에 일어나는 모든 일에 매우 세심한주의를 지불하지 않는 경우 예상치 못한 동작이 발생할 수있는 변수에 참조에 의해 그 ->bindParam 작품을 주목하는 것이 중요하다, 전체 코드가 아닙니다 에. 명시 적으로 참조가 필요하지 않은 경우 ->bindValue을 사용하면 (이름에서와 같이 변수를 값으로 바인딩 함) 더 안전하고 쉽습니다. 분명히 여기에서 효과가 있었지만, 정확히 은 완전한 코드 없이는 설명하기가 어렵습니다.)

+0

다시 한번 감사드립니다. 나는'bindParam'을 간과했다고 믿을 수 없다. – Brett

관련 문제