2016-08-31 8 views
0

"till.code"の値を取得し、インクリメンタル数値インデックス配列で保存したいと思います。 考えられるのは、 "$ array [$ variable]、$ array [$ variable] .. etc"でこれらの値を出力できるということです。変数は、最善の方法ではないかもしれない数字の数、1,2,3 ...msqlのフェッチを使用してPHP配列を埋めてください

$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.** 
    } 

答えて

1

簡単な方法を、する必要がありますが、それは次のようになり、シナリオによって異なります。

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

どのようにして2番目の値を配列から印刷できますか? echo $ arr [2]? – talendguy

+0

はい、2番目の行を意味する場合。行内に2番目の値が必要な場合は、 '$ arr [2] ['値の名前']'を使用する必要があります。他の配列と同じように '$ arr'にアクセスすることができます。 – RST

+0

値の名前は何ですか?インデックスとしての配列を変数として出力することはできませんか?私はecho $ arr [$ index] – talendguy

0

ありません確かに私は理解していますが、おそらくこのようなものです。物事をより簡単にするために、エイリアスを列に割り当てます。

$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; 
関連する問題