gpt4 book ai didi

php - PDO - 查询不返回结果

转载 作者:行者123 更新时间:2023-11-29 14:35:41 26 4
gpt4 key购买 nike

查询结果出现问题。 getSales() 函数在第一次调用时效果很好。再次调用时,查询不会产生任何结果。这是一小块代码:

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

当我调用 getSales() 获取 AM 或 PM 销售数字时,该函数成功返回数据。当我第二次调用它时,该函数不返回任何数据。不确定我是否需要释放结果或类似的东西。我尝试了 unset($sth)$sth->closeCursor(),但似乎都不能解决我的问题。任何帮助解决我的问题的帮助将不胜感激。如果需要更多详细信息,请告诉我。

最佳答案

由于这不是完整的代码,因此请务必注意,->bindParam 通过对变量的引用来工作,如果您没有非常密切地关注这些变量随后发生的一切。如果您没有明确需要引用,那么使用 ->bindValue (顾名思义,按值绑定(bind)变量)更安全、更简单,最重要的是更清晰。显然这在这里有效,但如果没有完整的代码,很难说清楚为什么;)

关于php - PDO - 查询不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9117828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com