1 回答

TA貢獻1802條經驗 獲得超5個贊
請耐心聽我說,因為自從我使用程序 mysqli 以來已經很久了......
如果您希望動態綁定列參數并執行獲取循環(因為 mysqlnd 不是一個選項),那么這里的代碼可以實現這一點。
mysqli_stmt_execute($stmt);
// now before you loop on $stmt->fetch, you must bind all the columns that exist
// to a row which will hold the values during the looping on fetch (yes, confusing)
$row = array(); // will hold each fetch'd loop result
$params = array(); // something to pass keyed references to $row with
$params[] = $stmt; // add the $stmt object as first param (for procedural way)
$meta = mysqli_stmt_result_metadata($stmt);// get what those columns will be
while($field = $meta->fetch_field()) {
if (!isset($row[ $field->name ])) { $row[ $field->name ] = null; } // set if not set
$params[] = &$row[ $field->name ]; // add reference to keyed row value
}
$meta->close();// metadata no longer needed, close it
call_user_func_array('mysqli_stmt_bind_result', $params);
while (mysqli_stmt_fetch($stmt)) {
// in here you then have $row to use as before
echo $row['id'];
}
現在,你可能會認為我在這里太過分了……是的。該示例用于處理未知的sql 列抓?。ㄊ褂肧ELECT *語法)。但是,如果您知道所有列都出來,則可以簡單地在 while 循環之前綁定每一列,如下所示:
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $col2, $col3);
while (mysqli_stmt_fetch($stmt)) {
// in here you then use $id, $col2, $col3
echo $id;
}
這就是為什么切換到該PDO庫可以使事情變得非常容易,因為它提供了在簡單循環中簡單地獲取具有關聯鍵名的行的規定,就像您所知道并喜歡的可用一樣mysqlnd。然而,如果PDO這也不是一個選項,那么您將面臨痛苦且神秘的方法來處理“binds”和“mysqli”。
- 1 回答
- 0 關注
- 171 瀏覽
添加回答
舉報