2014-01-20 54 views
0

我正在嘗試爲我的數據庫類編寫一個準備好的選擇查詢函數。我認爲從下面的代碼你可以理解我想要做的,但顯然這個代碼不起作用。如何做到這一點?準備選擇查詢功能

public function preparedSelectQuery($sql, $types){ 
    if(func_num_args() < 3){ 
     throw new Exception("Less than 3 args!"); 
    } 
    $stmt = $this->mysqli->stmt_init(); 
    if (!$stmt->prepare($sql)) { 
     throw new Exception("Failed to prepare statement!"); 
    } else { 
     if($stmt->bind_param($types, func_get_args())){ 
      if($stmt->execute()){ 
       $result = $stmt->get_result(); 
       while ($row = $result->fetch_assoc()) { 
        $dataset[] = $row; 
       } 
       $result->close(); 
       $stmt->close(); 
       return $dataset; 
      }else{ 
       throw new Exception("Failed to execute!"); 
      } 
     }else{ 
      throw new Exception("Failed to bind parameters!"); 
     } 
    } 
} 
+0

什麼是你得到的錯誤? 「只是不行」並沒有告訴我們任何事情。 – mcryan

+0

看起來像參數綁定有錯誤。 – BlitZ

+2

func_get_args()返回一個數組,它不是bind_param()的有效輸入 –

回答

0

答案:

function makeValuesReferenced($arr){ 
     $refs = array(); 
     foreach($arr as $key => $value) 
      $refs[$key] = &$arr[$key]; 
     return $refs; 

    } 
$funcArgs = array_slice(func_get_args(),1); 
call_user_func_array(array($stmt, 'bind_param'), makeValuesReferenced($funcArgs));