note 99175 added to mysqli-stmt.bind-result

From: Date: Sun, 01 Aug 2010 02:06:34 +0000
Subject: note 99175 added to mysqli-stmt.bind-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-171292@lists.php.net to get a copy of this message
If done properly, 'call_user_func_array()' works to bind variables to a result of multiple columns including BLOB columns. EXAMPLE: $data = array() ; // Array that accepts the data. $params = array() ; // Parameter array passed to 'bind_result()' $column = array("fidentity", "fvarchar", "fdate", "ftinyblob") ; // The column names. foreach($column as $col_name) { // 'fetch()' will assign fetched value to the variable '$data[$col_name]' $params[] =& $data[$col_name] ; } $res = call_user_func_array(array($stmt, "bind_result"), $params) ; Here's the complete example. WARNING: When using 'prepare' to prepare a statement to retrieve LOBs the method order matters. Also, method 'store_result()' must be called and be called in correct order. Failure to observe this causes PHP/MySQLi to crash or return an erroneous value. The proper procedure order is: prepare -> execute -> store_result -> bind -> fetch $database = "test" ; $table = "test" ; $column = array("fidentity", "fvarchar", "fdate", "ftinyblob") ; $select_set = "fidentity, fvarchar, fdate, ftinyblob" ; $mysqli = new mysqli("localhost", "root", $password, $database); // Proper procedure order: prepare -> execute -> store_result -> bind -> fetch $stmt = $mysqli->prepare("SELECT $select_set FROM $table") ; $stmt->execute(); $stmt->store_result(); $data = array() ; // Array that accepts the data. $params = array() ; // Parameter array passed to 'bind_result()' foreach($column as $col_name) { // Assign the fetched value to the variable '$data[$name]' $params[] =& $data[$col_name] ; } $res = call_user_func_array(array($stmt, "bind_result"), $params) ; if(! $res) { echo "bind_result() failed: " . $mysqli->error . "\n" ; } else { $res = $stmt->fetch() ; if($res) { echo "<pre>" . htmlentities(print_r($data, true)) . "</pre>\n" ; } else { echo ((false !== $res) ? "End of data" : $stmt->error) . "\n" ; } } $stmt->close() ; $mysqli->close() ; exit ; The above example should output: Array ( [fidentity] => 24 [fvarchar] => the rain in spain [fdate] => 2010-07-31 [ftinyblob] => GIF89a...(more BLOB data) ) ---- Server IP: 64.71.164.2 Probable Submitter: 131.191.11.159 ---- Manual Page -- http://www.php.net/manual/en/mysqli-stmt.bind-result.php Edit -- https://master.php.net/note/edit/99175 Del: integrated -- https://master.php.net/note/delete/99175/integrated Del: useless -- https://master.php.net/note/delete/99175/useless Del: bad code -- https://master.php.net/note/delete/99175/bad+code Del: spam -- https://master.php.net/note/delete/99175/spam Del: non-english -- https://master.php.net/note/delete/99175/non-english Del: in docs -- https://master.php.net/note/delete/99175/in+docs Del: other reasons-- https://master.php.net/note/delete/99175 Reject -- https://master.php.net/note/reject/99175 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#171292) next »