Home  >  Article  >  Backend Development  >  while 的变量传出去不一样?

while 的变量传出去不一样?

WBOY
WBOYOriginal
2016-06-23 13:57:46958browse

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; 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn