如果操作正確,`call_user_func_array()` 可以將變數綁定到多個欄位的結果,包含 BLOB 欄位。
範例
<?php
$data = array() ; $params = array() ; $column = array("fidentity", "fvarchar", "fdate", "ftinyblob") ; foreach($column as $col_name)
{
$params[] =& $data[$col_name] ;
}
$res = call_user_func_array(array($stmt, "bind_result"), $params) ;
?>
以下是完整的範例。
警告:使用 `prepare` 函式準備提取大型物件 (LOB) 的語句時,方法的呼叫順序非常重要。
此外,`store_result()` 方法必須被呼叫,且必須以正確的順序呼叫。
未遵守此順序將導致 PHP/MySQLi 崩潰或返回錯誤的值。
正確的程序順序為:prepare -> execute -> store_result -> bind -> fetch
<?php
$database = "test" ;
$table = "test" ;
$column = array("fidentity", "fvarchar", "fdate", "ftinyblob") ;
$select_set = "`fidentity`, `fvarchar`, `fdate`, `ftinyblob`" ;
$mysqli = new mysqli("localhost", "root", $password, $database);
$stmt = $mysqli->prepare("SELECT $select_set FROM `$table`") ;
$stmt->execute();
$stmt->store_result();
$data = array() ; $params = array() ; foreach($column as $col_name)
{
$params[] =& $data[$col_name] ;
}
$res = call_user_func_array(array($stmt, "bind_result"), $params) ;
if(! $res)
{
echo "bind_result() 失敗: " . $mysqli->error . "\n" ;
}
else
{
$res = $stmt->fetch() ;
if($res)
{
echo "<pre>" . htmlentities(print_r($data, true)) . "</pre>\n" ;
}
else
{
echo ((false !== $res) ? "資料結束" : $stmt->error) . "\n" ;
}
}
$stmt->close() ;
$mysqli->close() ;
exit ;
?>
以上範例應該輸出
陣列 (
[fidentity] => 24
[fvarchar] => the rain in spain
[fdate] => 2010-07-31
[ftinyblob] => GIF89a...(更多 BLOB 資料)
)