2013-03-01 100 views
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 $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; 

,但我不能让导致

+0

'$ id = $ cashier_data ['cashierId']' – ficuscr 2013-03-01 20:17:55

+0

如果确实回答了您的问题 – michi 2013-04-14 12:57:18

回答

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']

+0

,请考虑接受答案(点击左侧的勾号标记),但此功能n是名为Bills的类的函数,我怎样才能得到结果 – 2013-03-01 21:34:03

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; 
0
//$UserID - your user's id 
$result = GetCashierDetail ($UserID); 
//now just using as normal array, for example 
echo $result['cashierId']; 
+0

,但这个函数是名为Bills的类的函数,我如何得到结果,我试试这个$ BillDraftSubmit = new Bills; $ BillDraftSubmit-> GetCashierDetail(71); – 2013-03-01 21:37:11

2

如何:

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

非主流解决方案:P +1 – Sam 2013-03-01 20:21:07

+0

@Sam:我认为它与我经常使用的wordpress上瘾密切相关;) – 2013-03-01 20:22:25

+0

更好的防御使用'list($ cashierId,$ CashierTotal,$ CashierLastTotal)= GetCashierDetail(11) ;' – webbiedave 2013-03-01 21:47:13

0

如果我明白你的问题正确,要显示出纳ID的价值。

你能做到这样的..

$cashierData = GetCashierDetail(11); 

$ID = $cashierData['cashierId']; 

echo 'cashier ID is: ' . $ID; 

同样你也可以通过$cashierData['CashierTotal']得到CashierTotal等

0

我不知道我是否肠道你的权利......

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

echo $id; 
+1

逗号是多余的。 – webbiedave 2013-03-01 21:44:42

+0

@webbiedave对,谢谢 – michi 2013-03-01 21:46:57