简体   繁体   中英

Postgresql php function to use value from one query in another

I need to get one select value $code into another in a function, but keep breaking everything. This is the function.

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;
}

for your code .. you should not use the return in the middle of the function .. because return end the excution of the function and "return" the value to a function caller ..the code after return will never executed

Anyway in your case you could also avoid the use of two query just using

select staff.code, outlet.name 
from staff  
INNER JOIN outlet  ON  (staff.code = outlet.code  
    and staff.user = 14 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM