0
在寫一個登錄系統的web項目IM在我的工作中的某些部分是如何碰到的結合未知數量的參數問題,發現在PHP手冊頁此功能。我總是喜歡完全理解一些代碼,我把它放到我正在處理的任何東西中,而我很難理解這個函數的幾個部分是如何工作的。不確定該功能的工作
伊夫評論的一切,我想我明白了(如果我錯了,請讓我知道),並留下了我的意見主要問題:
<?php
function getresult($stmt) {
//Define var for holding the result
$result = array();
//asign metadata of the statments result
$metadata = $stmt->result_metadata();
//grab the feilds from the metadata and assign to var
$fields = $metadata->fetch_fields();
//for loop with internal break
for (;;) {
//pointers array (not sure why this is an array and not a stdClass)
$pointers = array();
//row empty class
$row = new stdClass();
//set pointers array to the value of the passed statement (casting $pointers to mysqli_stmt class I assume(?)
$pointers[] = $stmt;
//iterate through all fields
foreach ($fields as $field) {
//each time set $fieldname to the name from the current element of $fields
$fieldname = $field->name;
//?? this is my big issue no idea whats going on here $row hasnt been set from what i can see, and no idea why its being refered to by reference and not value
$pointers[] = &$row->$fieldname;
}
//call bind result for all values
call_user_func_array(mysqli_stmt_bind_result, $pointers);
//internal break if
if (!$stmt->fetch()) {
//if there is nothing left to fetch break
break;
}
//set the result
$result[] = $row;
}
//free resources
$metadata->free();
//return result
return $result;
}
?>
提前感謝!
因此,每次循環時,都會使用字段名稱將$ pointers []的新元素設置到$ row的內存位置,然後使用call_user_func_array()將$ pointers數組傳遞給mysqli_stmt_bind_result(),然後返回$ row對象而不是$指針數組。只有call_user_func_array()需要$指針。是嗎? – 2012-04-02 08:31:34
是的,'$ pointers'包含對'$ row'中字段的引用。 – ThiefMaster 2012-04-02 08:36:04
謝謝,我認爲它:) – 2012-04-03 08:15:58