从函数返回多个值

时间:2013-03-01 20:15:38

标签: php

我有这个函数,在调用它时会返回三个值。

function GetCashierDetail ($UserID){
        $GetCashierID = "SELECT cashiers_CashierID,cashiers_Total,cashiers_Last_Total 
        FROM `cashiers` 
        WHERE `cashiers_CashierCloseDate` is null and `cashiers_Status`='0' 
        and `cashiers_Delete` = '0' and `cashiers_User` = '".$UserID."'";
        $objQueryCashierID = mysql_query($GetCashierID) or die ("Error Query [".$strSQL."]");
        $GetCashierIDResult = mysql_fetch_array($objQueryCashierID);
        $BillsCashierID = $GetCashierIDResult['cashiers_CashierID'];
        $CashierTotal=$GetCashierIDResult['cashiers_Total']; 
        $CashierLastTotal=$GetCashierIDResult['cashiers_Last_Total']; 
        $num=mysql_affected_rows();
        // Return Data
        return $cashier_data = array('cashierId'=>$BillsCashierID ,
                          'CashierTotal'=>$CashierTotal ,
                          'CashierLastTotal'=>$CashierLastTotal );                      

}

现在调用此函数时GetCashierDetail (11)我需要在这个变量上打印$cashier_data

$ID = $BillsCashierID

以其他方式使用它。

我会尝试

class Bills {
    // Get Cashier ID
function GetCashierDetail ($ausers_ID){
        $GetCashierID = "SELECT cashiers_CashierID,cashiers_Total,cashiers_Last_Total 
        FROM `cashiers` 
        WHERE `cashiers_CashierCloseDate` is null and `cashiers_Status`='0' 
        and `cashiers_Delete` = '0' and `cashiers_User` = '".$UserID."'";
        $objQueryCashierID = mysql_query($GetCashierID) or die ("Error Query [".$strSQL."]");
        $GetCashierIDResult = mysql_fetch_array($objQueryCashierID);
        $BillsCashierID = $GetCashierIDResult['cashiers_CashierID'];
        $CashierTotal=$GetCashierIDResult['cashiers_Total']; 
        $CashierLastTotal=$GetCashierIDResult['cashiers_Last_Total']; 
        $num=mysql_affected_rows();
        // Return Data
        return $cashier_data = array('cashierId'=>$BillsCashierID ,
                          'CashierTotal'=>$CashierTotal ,
                          'CashierLastTotal'=>$CashierLastTotal);
}

}

$BillDraftSubmit = new Bills();
$data=$BillDraftSubmit->GetCashierDetail(71);
$cashierId=$data["cashierId"];
$CashierTotal=$data["CashierTotal"];
$CashierLastTotal=$data["CashierLastTotal"];
echo "cashierId".$cashierId;
echo "<br>CashierTotal".$CashierTotal;
echo "<br>CashierLastTotal".$CashierLastTotal;

但我无法得到结果

7 个答案:

答案 0 :(得分:2)

怎么样:

$arr = GetCashierDetail (11);
extract($arr);
print $cashierId." ".$CashierTotal." ".$CashierLastTotal;

答案 1 :(得分:1)

$return = GetCashierDetail(11);
$id = $return['cashierId'];

RTM:http://php.net/manual/en/language.types.array.php

答案 2 :(得分:0)

只返回数组,无需将其分配给任何内容。即。

return array('cashierId'=>$BillsCashierID ,
                      'CashierTotal'=>$CashierTotal ,
                      'CashierLastTotal'=>$CashierLastTotal ); 

或做

$cashier_data = array('cashierId'=>$BillsCashierID ,
                      'CashierTotal'=>$CashierTotal ,
                      'CashierLastTotal'=>$CashierLastTotal ); 
return $cashier_data;

然后当您使用$cd = GetCashierDetail(1);返回时,您可以访问$cd['CashierTotal']

答案 3 :(得分:0)

为什么不返回一个数组?

        function GetCashierDetail ($UserID){
    $GetCashierID = "SELECT cashiers_CashierID,cashiers_Total,cashiers_Last_Total 
    FROM `cashiers` 
    WHERE `cashiers_CashierCloseDate` is null and `cashiers_Status`='0' 
    and `cashiers_Delete` = '0' and `cashiers_User` = '".$UserID."'";
    $objQueryCashierID = mysql_query($GetCashierID) or die ("Error Query [".$strSQL."]");
    $GetCashierIDResult = mysql_fetch_array($objQueryCashierID);
    $BillsCashierID = $GetCashierIDResult['cashiers_CashierID'];
    $CashierTotal=$GetCashierIDResult['cashiers_Total']; 
    $CashierLastTotal=$GetCashierIDResult['cashiers_Last_Total']; 
    $num=mysql_affected_rows();
    // Return Data
    return array('cashierId'=>$BillsCashierID ,
                      'CashierTotal'=>$CashierTotal ,
                      'CashierLastTotal'=>$CashierLastTotal );                      

}  

$user = GetCashierDetail($id);
$ID = $user['cashierId'];
echo $ID;

答案 4 :(得分:0)

//$UserID - your user's id
$result = GetCashierDetail ($UserID);
//now just using as normal array, for example
echo $result['cashierId'];

答案 5 :(得分:0)

如果我正确理解了您的问题,您希望显示收银员ID的值。

你可以这样做..

$cashierData = GetCashierDetail(11);

$ID = $cashierData['cashierId'];

echo 'cashier ID is: ' . $ID;

同样,您也可以通过$cashierData['CashierTotal']获得CashierTotal,依此类推

答案 6 :(得分:0)

我不确定我是否把你弄好了......

list($id,,) = GetCashierDetail(11);

echo $id;