2017-01-31 19 views
0

ループ中にこの配列の最後の値しか取得できないのはなぜですか?すべての記録を保持してはいけませんか?クエリのすべての値で配列が構築されないのはなぜですか?ループの外側にある配列の値を取得する

これは私のコードです。

$cost_array = array(); 
if ($stmt = $conn_mysqli -> prepare("SELECT costs_items.cno, costs_items.name,costs_items.description, costs_items.type,costs_items.measure, costs_items.amount, costs_items.vc_fc,costs_items_custom.cno, costs_items_custom.name, costs_items_custom.description, costs_items_custom.type, costs_items_custom.measure, costs_items_custom.amount, costs_items_custom.vc_fc FROM costs_items LEFT JOIN costs_items_custom ON costs_items.costs_id = costs_items_custom.costs_id WHERE costs_items.costs_id = ?")) { 
     $stmt -> bind_param("i", $costs_select); 
     $stmt -> execute(); 
     $stmt -> bind_result($cno, $cost_name, $cost_description, $cost_type, $cost_measure, $cost_amount, $cost_vcfc, $ccustom_cno, $ccustom_name, $ccustom_description, $ccustom_type, $ccustom_measure, $ccustom_amount, $ccustom_vc_fc); 
     while($stmt -> fetch()){ 

      //Here we put all costs in array 
      $cost_array['cno'] = $cno; 
      $cost_array['cost_name'] = $cost_name; 
      $cost_array['cost_description'] = $cost_description; 
      $cost_array['cost_type'] = $cost_type; 
      $cost_array['cost_measure'] = $cost_measure; 
      $cost_array['cost_amount'] = $cost_amount; 
      $cost_array['cost_vcfc'] = $cost_vcfc; 

     } 
     $stmt -> close();          } 

    var_dump($cost_array); 

答えて

1

コードを次のように変更します。[]を使用して新しいインデックスを作成します。

$cost_array = array(); 
while($stmt -> fetch()){ 
    $cost_array['cno'][] = $cno; 
    $cost_array['cost_name'][] = $cost_name; 
    $cost_array['cost_description'][] = $cost_description; 
    $cost_array['cost_type'][] = $cost_type; 
    $cost_array['cost_measure'][] = $cost_measure; 
    $cost_array['cost_amount'][] = $cost_amount; 
    $cost_array['cost_vcfc'][] = $cost_vcfc; 

} 
$stmt -> close();   

var_dump($cost_array);//all your values 
+0

を試してみてください。ありがとうございました! – Budimir

1

これだけです。この方法

$cost_array[] = array(
     'cno' => $cno, 
     'cost_name' => $cost_name, 
     'cost_description' => $cost_description 
    ); 
関連する問題