$output = '';
mysql_connect("localhost","root",123456);
mysql_select_db("temp");
mysql_query("set names gbk");
function get_path($nod)
{
    $result = mysql_query("select * from test where id = " . $nod);
    if(mysql_num_rows($result))
    {
        $rows = mysql_fetch_array($result);
        $output = $rows["name"];
        $nod = $rows["fid"];
        get_path($nod);
    }
}
get_path(4);
echo $output;

解决方案 »

  1.   

    一楼的哪个叫你用全局变量了啊?如果用全局变量的话,哪个人也能想得出来啊?
    不能用全局变量,只能用局部变量,也不能用:用if($rows["fid"] == 0)这个条件来判断
    哈哈,
      

  2.   

    根据你所给的数据我这里可以通过.
    <?php
    $output = ''; 
    mysql_connect("localhost","root","123456"); 
    mysql_select_db("temp"); 
    mysql_query("set names gbk");
    function get_path($nod) 


        $result = mysql_query("select * from test where id = '$nod';"); 

        if(mysql_num_rows($result)) 
        { 
            $rows = mysql_fetch_array($result); 

            $output = $rows["name"]; 
        $nod = $rows["fid"]; 
            get_path($nod); 
    }
    return $output;
    }
    $new = get_path(4); 
    echo $new;
    ?>
      

  3.   

    $query='select name from $table where fid not in(select id from $table)';
    这个是不是最顶层的,干嘛那么费事?
    不过你那好像是递归输出所有的!