为什么我只在while循环中获取此数组中的最后一个值。它不应该记录所有记录吗?为什么数组不是使用查询中的所有值构建的?
这是我的代码。
$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);
答案 0 :(得分: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
答案 1 :(得分:1)
试试这种方式
$cost_array[] = array(
'cno' => $cno,
'cost_name' => $cost_name,
'cost_description' => $cost_description
);