2012-02-02 19 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()を呼び出し、関数が正常にデータを返します。ここではコードの小さな塊です。私はそれを2回目に呼び出すと、関数はデータを返しません。結果や何かをそれらの行に沿って解放する必要があるかどうかはわかりません。私はunset($sth)$sth->closeCursor()を試しましたが、どれも私の問題を解決していないようです。私の問題を解決する助けがあれば大歓迎です。詳細が必要な場合はお知らせください。

+0

「$ this-> totalLocations」は定義されておらず、 – Wrikken

+0

私はそれが設定されているコードを含んでいませんでした。この質問の目的のために。 '$ this-> totalLocations' = 20 – Brett

+0

あなたの問題とは関係ありませんが、try/catchを乱用しているようです。そのレベルに何も投げられないので、getSales()のキャッチは起こりません。 – Kenaniah

答えて

1

、これは全体のコードではないので、あなたが後でそれらの変数に起こるすべてに非常に細心の注意を払っていない場合は予期しない動作が発生する可能性があり、変数へ参照によって->bindParam作品ということに注意することが重要ですに。明示的に参照を必要としない場合は、->bindValue(名前が示すように変数を値でバインドする)を使用すると、より安全で簡単です。明らかにこれはここではうまくいったが、正確にはなぜは完全なコードなしでは説明するのが難しい;)

+0

もう一度ありがとう。私は 'bindParam'を見落としたとは思えません。 – Brett

関連する問題