简体   繁体   中英

How to find rows from less than 6 months ago in PHP and MySQL, Month By Month

i have this SQL sentence when execute in phpmyadmin result is fine:

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

Result:

COUNT(cid)

221
380
368
315
140
204
54

But when i try in PHP only obtain one count

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

Actual Result: 221

Expected:

221
380
368
315
140
204
54

Thanks in Advance... :)

Check below code:

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

Hope it helps you.

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