简体   繁体   中英

PDO - query not returning results

Having trouble with query results. The getSales() function works great the first time it is called. When called again, the query produces no results. Here is a small chunk of code:

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);

When I call getSales() for AM or PM sales numbers, the function successfully returns the data. When I call it a second time, the function doesn't return any data. Not sure if I need to free the results or something along those lines. I tried unset($sth) and $sth->closeCursor() , none of which seems to fix my problem. Any help with fixing my issue would be greatly appreciated. Please let me know if more details are needed.

As this is not the whole code, it is important to note that ->bindParam works by reference to a variable, which could result in unexpected behavior if you are not paying very close attention to everything that happens to those variables later on. If you don't explicitly need a reference, using ->bindValue (which, as the name states, binds the variable by value) is safer, easier and most of all much clearer. Apparently this worked here, but exactly why is hard to state without complete code ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM