【问题标题】:How to find rows from less than 6 months ago in PHP and MySQL, Month By Month如何在 PHP 和 MySQL 中逐月查找不到 6 个月前的行
【发布时间】:2019-02-14 06:27:07
【问题描述】:

在phpmyadmin中执行时我有这个SQL语句结果很好:

SELECT COUNT(cid) as total from courier where pick_date > DATE_SUB(now(),
INTERVAL 6 MONTH) group by year(pick_date), MONTH(pick_date)

结果:

COUNT(cid)

221
380
368
315
140
204
54

但是当我在 PHP 中尝试时只获得一个计数

$out = array();
$sql="SELECT COUNT(cid) as total from courier where pick_date > DATE_SUB(now(), INTERVAL 6 MONTH) group by year(pick_date), MONTH(pick_date)";
$pquery=$pdo->query($sql);
$prow=$pquery->fetch_all(PDO::FETCH_ASSOC);
$out[]=$prow['total'];
echo implode( ", ", $out );

实际结果:221

Expected:

221
380
368
315
140
204
54

在此先感谢... :)

【问题讨论】:

  • 这里 cids area different & you are getting its count only... Please post the result with cids 也...所以很容易找到解决方案
  • ->fetch_all() 是你的包装函数吗?因为标准是->fetchAll()。此外,由于->fetchAll() 返回一个数组数组,因此您必须循环遍历结果

标签: php mysql loops


【解决方案1】:

检查以下代码:

$out = array();
$sql="SELECT COUNT(cid) as total from courier where pick_date > DATE_SUB(now(), INTERVAL 6 MONTH) group by year(pick_date), MONTH(pick_date)";
$stmt = $pdo->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC); 

$out = array();
foreach($stmt->fetchAll() as $value) {
    $out[] = $value['total'];
}
echo implode(", ", $out );

希望对你有帮助。

【讨论】:

  • 祝你好运@josav。
猜你喜欢
  • 1970-01-01
  • 2022-11-22
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多