Rumah > Artikel > pembangunan bahagian belakang > while 的变量传出去不一样?
function type_son_id_finder($type_id){ $query=mysql_query("SELECT id FROM `protduct_type` WHERE `f_id` = '$type_id'"); while ($row=mysql_fetch_assoc($query)) { return $all_son = $row["id"].","; }}
建议先学习下基础知识吧
function type_son_id_finder($type_id){
$query=mysql_query("SELECT id FROM `protduct_type` WHERE `f_id` = '$type_id'");
$all_son = '';
while ($row=mysql_fetch_assoc($query)) {
$all_son .= $row["id"].",";
}
return $all_son;
}
return放在循环里面 就会跳出当前循环 所以只执行一遍循环就结束了 因此只能得到一个结果1
正确结果可以参考#2 return放在循环之后
function type_son_id_finder($type_id){
$query=mysql_query("SELECT id FROM `protduct_type` WHERE `f_id` = '$type_id'");
$all_son = '';
while ($row=mysql_fetch_assoc($query)) {
$all_son .= $row["id"].",";
}
return $all_son;
}
return $all_son = $row["id"].",";
???每次都?覆?$all_son 所以只?返回最後一???的值,
改成 $all_son . = $row["id"].",";
在循??束?再 return $all_son;