用msql fetch填充php数组


Fill php array with msql fetch

我想要得到"till "。编码"值",并将其保存为增量数字索引数组。我的想法是,我可以用"$array[$variable],$array[$variable]…等"来打印这些值。变量应该是一个数字,1、2、3…

 $stmt2=$dbh->prepare("SELECT till.code,shop.description Collate Cyrillic_General_CI_AI as description, count(tra.id) as servicios,ROUND(sum(tra.total_amount),1) as facturacion
          FROM [TCPOS4].[dbo].[transactions] as tra, [TCPOS4].[dbo].[tills] as till,[TCPOS4].[dbo].[shops] as shop where tra.till_id=till.id and shop.id=till.shop_id  and convert(date,tra.trans_date) = '$name2' and '$usuari'=CAST(LEFT(shop.code,5) AS INT) group by till.code,shop.description order by code;");
    $stmt2->execute();
    while ($row = $stmt2->fetch()) {
         **I have to fill this.**
    }

最简单的方法(可能不是最好的方法,但这取决于具体情况)是:

$index = 1;
$arr = array();
while ($row = $stmt2->fetch()) {
     $arr[ $index ] = $row;
     $index++;
}

不确定我是否理解,但也许是这样的。为列指定别名以使操作更简单。

$stmt2=$dbh->prepare("SELECT 
                        till.code as 'code',
                        shop.description Collate Cyrillic_General_CI_AI as 'description', 
                        count(tra.id) as 'servicios',
                        ROUND(sum(tra.total_amount),1) as 'facturacion'
                    FROM
                        [TCPOS4].[dbo].[transactions] as tra, 
                        [TCPOS4].[dbo].[tills] as till,
                        [TCPOS4].[dbo].[shops] as shop 
                    WHERE tra.till_id=till.id
                            and shop.id=till.shop_id 
                            and convert(date,tra.trans_date) = '$name2' 
                            and '$usuari'=CAST(LEFT(shop.code,5) AS INT) 
                    GROUP BY till.code,shop.description
                    ORDER BY code;");
    $stmt2->execute();
    $output=array();
    while( $row = $stmt2->fetch() )$output[]=$row->code;