Heim  >  Artikel  >  Backend-Entwicklung  >  while 的变量传出去不一样?

while 的变量传出去不一样?

WBOY
WBOYOriginal
2016-06-23 13:57:46955Durchsuche

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"].",";        }}

这个方法是输入一个父栏目ID 就查找到有什么子栏目ID

以上的方法

应该是输出

1,2,

但只能输出
1,

但如果把
                return $all_son = $row["id"].",";
改为:
                echo $all_son = $row["id"].",";

就能正常的显示出1,2,

这是为什么?

我如何解决?
我是想把这个方法最终输出成

1,2

让我在mysql中 WHERE id IN (1,2) 

但现在只能输出第一个,还不能用return, 只能用echo?

求解!


回复讨论(解决方案)

建议先学习下基础知识吧


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; 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn