note 99175 modified in mysqli-stmt.bind-result by danbrown

From: Date: Sun, 01 Aug 2010 14:04:46 +0000
Subject: note 99175 modified in mysqli-stmt.bind-result by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-171307@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: <?php $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 <?php $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) ) --was-- 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) ) http://php.net/manual/en/mysqli-stmt.bind-result.php

« previous php.notes (#171307) next »