2014-04-15 95 views
-2

所以我一直在尋找一個解決方案,用php動態綁定參數,這裏是我做的一個函數。用mysqli bind_param動態綁定參數

public function getEntity($tableName, $fieldList, $keyValueArray = NULL) 
{ 
    // First construct the sql to be used to get the entity 
    if(is_array($fieldList)) 
    { 
     $sql = "SELECT "; 
     foreach($fieldList as $field) 
     { 
      $sql .= $field." , "; 
     } 
     $sql = rtrim($sql, ' , '); 
    } 
    else 
    { 
     $sql = "SELECT ".$fieldList; 
    } 
    $sql .= " FROM ".$tableName; 
    if($keyValueArray != NULL) 
    { 
     $sql .= " WHERE "; 
     foreach($keyValueArray as $key => $value) 
     { 
      $sql .= $key." = ? AND "; 
     } 
     $sql = rtrim($sql, ' AND '); 
    } 
    //Then grab the connection 
    $connection = $this->getConnection(); 
    //Prepare the statement 
    $stmt = $connection->prepare($sql); 
    if($stmt === false) 
    { 
     trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $connection->errno . ' ' . $connection->error, E_USER_ERROR); 
    } 
    // Bind the paramaters 
    if($keyValueArray != NULL AND count($keyValueArray) < 2) 
    { 
     $stmt->bind_param("s", $keyValueArray[key($keyValueArray)]); 
    } 
    elseif($keyValueArray != NULL) 
    { 
     $valueArray = array(); 
     $string = NULL; 
     for($i = 0; $i < count($keyValueArray); $i++) 
     { 
      $string .= "s"; // for temp solution  
     } 
     $valueArray[] = &$string; 
     foreach($keyValueArray as $key => $value) 
     { 
      $valueArray[] = &$keyValueArray[$key]; 
     } 
     call_user_func(array($stmt, 'bind_param'), $valueArray); 
    } 
    //Execute the statement 
    $stmt->execute(); 
    //Grab the results and stuff into multidimensional array 
    $data = $stmt->result_metadata(); 
    $fields = array(); 
    $currentrow = array(); 
    $results = array(); 
     //Store references to keys in $currentrow 
    while ($field = mysqli_fetch_field($data)) 
    { 
     $fields[] = &$currentrow[$field->name]; 
    } 
     // Bind statement to $currentrow using the array of references 
    call_user_func_array(array($stmt, 'bind_result'), $fields); 
     // Iteratively refresh $currentrow array using "fetch", store values from each row in $results array 
    $i = 0; 
    while ($stmt->fetch()) 
    { 
     $results[$i] = array(); 
     foreach($currentrow as $key => $val) 
     { 
     $results[$i][$key] = $val; 
     } 
    $i++; 
    } 
    $connection->close(); 
    return $results;  
} 

我然後運行這個函數是這樣的:

$keyValueArray['firstName'] = 'John'; 
$keyValueArray['lastName'] = 'Doe'; 
$fieldListArray[] = 'firstName'; 
$fieldListArray[] = 'lastName'; 
print_r($db->getEntity('ACCOUNT', $fieldListArray, $keyValueArray)); 

然而,當我運行的功能。我得到那個說

Wrong parameter count for mysqli_stmt::bind_param() 

我不知道我在做什麼錯了一個錯誤。任何幫助,將不勝感激。

+0

值得一提的是,對於這種類型的任務,使用PDO要容易得多,因爲您可以將一個值數組傳遞給'execute()'。不需要綁定! –

+0

-1。如果你的所有查詢都是由外部變量組成的,爲什麼還要用準備好的語句打擾呢 –

+0

在這種情況下,您的預先準備的陳述會對您造成傷害嗎? – AlexHeuman

回答

1

當您撥打bind_param時,您需要使用call_user_func_array而不是call_user_func

+0

謝謝,由於某種原因,我以爲我嘗試過,並沒有奏效。它現在可以工作了。 – AlexHeuman