Postgresql php函数在另一个查询中使用值

时间:2018-11-03 04:51:51

标签: php postgresql function

我需要在一个函数中将一个选择值$ code转换为另一个,但是要继续破坏所有内容。这是功能。

function agent_specific()
{
    $dbh = dbh_get();
    $code = '';
    $options = '';

    $sql = 'select code from staff where user = 14';
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    $r = $stmt->fetch();
    return $code;

    $sql = 'select code, name from outlet
        where code = ' . $code . '';
    $stmt = $dbh->prepare($sql);
    $stmt->execute();
    while (true) {
        $r = $stmt->fetch();
        if (is_bool($r)) break;
        $options .= '<option value="' . $r['code'] . '">' . $r['name'] . '</option>';
    }
    dbh_free($dbh);
    return $options;
}

1 个答案:

答案 0 :(得分:0)

对于您的代码..您不应在函数中间使用return ..因为return结束了函数的执行并将值“返回”给函数调用者.. return之后的代码将永远不会执行< / p>

无论如何,您也可以仅使用

避免使用两个查询
select staff.code, outlet.name 
from staff  
INNER JOIN outlet  ON  (staff.code = outlet.code  
    and staff.user = 14 )