为什么我的功能没有返回任何东西?

时间:2011-12-14 15:04:00

标签: php function

这是我的功能

function get_products($business_id){
    $sql = "SELECT
          b.*, b.name as business_name, p.builder_id,
          bs.*, p.price, p.name as product_name, p.description, p.image, p.per_station, p.skip_screen
        FROM 
          builder_product bp
        JOIN builder_step bs ON bp.builder_step_id = bs.builder_step_id
      JOIN builder b ON bp.builder_id = b.builder_id
        WHERE b.business_id = '{$business_id}' 
        ORDER by b.step_number, b.price";
    $result = mysql_query($sql);
    $steps = array();
    while($row_r = mysql_fetch_assoc($result)){
        $steps["all_steps"]['business_id'] = $business_id;
        $steps["business_name"] = $row_r['business_name'];
        $steps["builder_id"] = $row_r['builder_id'];

    if (count($steps) == 0) {   
        $business_id = 59;
        get_products($business_id);
    }else{
        $steps["business_id"] = $business_id;
        return $steps;
    }
}

这是对函数的调用

$products = get_products($_GET['business_id']);

为什么我没有从这个函数返回....我在返回之前做了一个print_r并且我的数组有大量的数据但是如果我在调用这个函数的下面做一个print_r我得到没有。我正在做一个递归调用,以确保我有一个返回数组...任何想法我缺少

5 个答案:

答案 0 :(得分:1)

我认为你需要在代码的if (count($steps) == 0)分支中添加一个return语句:

if (count($steps) == 0) {   
    $business_id = 59;
    return get_products($business_id);
}else{
    $steps["business_id"] = $business_id;
    return $steps;
}

答案 1 :(得分:0)

if (count($steps) == 0) {   
    $business_id = 59;
    get_products($business_id);
}else{
    $steps["business_id"] = $business_id;
    return $steps;
}

当你点击if而不是else时,不会返回任何内容。

编辑:将其更改为return get_products($business_id)

答案 2 :(得分:0)

如果出现以下情况,你就没有回复:

if (count($steps) == 0) {   
        $business_id = 59;
        get_products($business_id);
        return $whatYouwant;
    }else{
        $steps["business_id"] = $business_id;
        return $steps;
    }

答案 3 :(得分:0)

这是整个功能吗?看起来你错过了一个结束括号:

function get_products($business_id){
    $sql = "SELECT
          b.*, b.name as business_name, p.builder_id,
          bs.*, p.price, p.name as product_name, p.description, p.image, p.per_station, p.skip_screen
        FROM 
          builder_product bp
        JOIN builder_step bs ON bp.builder_step_id = bs.builder_step_id
      JOIN builder b ON bp.builder_id = b.builder_id
        WHERE b.business_id = '{$business_id}' 
        ORDER by b.step_number, b.price";
    $result = mysql_query($sql);
    $steps = array();
    while($row_r = mysql_fetch_assoc($result)){
        $steps["all_steps"]['business_id'] = $business_id;
        $steps["business_name"] = $row_r['business_name'];
        $steps["builder_id"] = $row_r['builder_id'];

        if (count($steps) == 0) {   
           $business_id = 59;
           get_products($business_id);
        }else{
           $steps["business_id"] = $business_id;
          return $steps;
        }
    }
}

答案 4 :(得分:0)

我找到了解决方案......

if (count($steps) == 0) {   
    $business_id = 59;
    $steps = get_products($business_id);
    $steps["business_id"] = $business_id;
    return $steps;
}else{
    $steps["business_id"] = $business_id;
    return $steps;
}

我需要从数组中的函数中获取返回值