2015-07-21 60 views
-1

到目前爲止我已經到了這裏。php sql multi bind_param

我的目標是能夠應用可選的搜索過濾器,如a.chainid和a.branchid(如果需要的話),爲此我需要動態bind_param。

該代碼似乎很好,但實際上它不工作的原因。

小心告訴我什麼是錯的?

該提取返回我什麼,而不是5行,應該。

在此先感謝

$sql = "SELECT a.COUPONID, a.TRUSTANDUSEID FROM `custom_redemptions` a WHERE a.couponid = 3"; 

    $types = ''; 
    $params = array(&$types); 

    if ($branchid != null) { 
     $sql .= "AND a.branchid = ?"; 
     $types .= 's'; 
     $params[] = $branchid; 
    } 
    if ($chainid != null) { 
     $sql .= "AND a.chainid = ?"; 
     $types .= 's'; 
     $params[] = $chainid; 
    } 

    if ($stmt = $this->dbCon->prepare($sql)) { 
     call_user_func_array(array($stmt, 'bind_param'), $params); 
     $stmt->execute(); 
     $stmt->bind_result($couponid, $trustanduseid); 
     while ($stmt->fetch()) { echo $couponid; } 
     $stmt->close(); 
    } 
+0

嘗試在SQL語句中的where之前刪除'a'。 – fsn

+0

仍然不能正常工作:/問題應該是關於call_user_func_array – hello

回答

0

該解決方案可以幫助你

$sql = "SELECT a.COUPONID, a.TRUSTANDUSEID FROM `custom_redemptions` a WHERE a.couponid = 3"; 

    $types = ''; 
    $params = array(&$types); 

    if ($branchid != null) { 
     $sql .= " AND a.branchid = ?"; 
     $types .= 's'; 
     $params[] = $branchid; 
    } 
    if ($chainid != null) { 
     $sql .= " AND a.chainid = ?"; 
     $types .= 's'; 
     $params[] = $chainid; 
    } 

    if ($stmt = $this->dbCon->prepare($sql)) { 
     call_user_func_array(array($stmt, 'bind_param'), $params); 
     $stmt->execute(); 
     $stmt->bind_result($couponid, $trustanduseid); 
     while ($stmt->fetch()) { echo $couponid; } 
     $stmt->close(); 
    } 
0

最後需要的是& $ chainid而不是$ chainid。

花了8小時研究它。 LOL