Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : MySQL Improved Extension : mysqli_stmt_bind_result

mysqli_stmt_bind_result

Binds variables to a prepared statement for result storage (PHP 5)
bool mysqli_stmt_bind_result ( mysqli_stmt stmt, mixed &var1 [, mixed &...] )

Example 1558. Object oriented style

<?php
$mysqli
= new mysqli("localhost", "my_user", "my_password", "world");

if (
mysqli_connect_errno()) {
   
printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

/* prepare statement */
if ($stmt = $mysqli->prepare("SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) {
   
$stmt->execute();

   
/* bind variables to prepared statement */
   
$stmt->bind_result($col1, $col2);

   
/* fetch values */
   
while ($stmt->fetch()) {
       
printf("%s %s\n", $col1, $col2);
   }

   
/* close statement */
   
$stmt->close();
}
/* close connection */
$mysqli->close();

?>

Example 1559. Procedural style

<?php
$link
= mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (!$link) {
   
printf("Connect failed: %s\n", mysqli_connect_error());
   exit();
}

/* prepare statement */
if ($stmt = mysqli_prepare($link, "SELECT Code, Name FROM Country ORDER BY Name LIMIT 5")) {
   
mysqli_stmt_execute($stmt);

   
/* bind variables to prepared statement */
   
mysqli_stmt_bind_result($stmt, $col1, $col2);

   
/* fetch values */
   
while (mysqli_stmt_fetch($stmt)) {
       
printf("%s %s\n", $col1, $col2);
   }

   
/* close statement */
   
mysqli_stmt_close($stmt);
}

/* close connection */
mysqli_close($link);
?>

Code Examples / Notes » mysqli_stmt_bind_result

anonymous

[[Editor's Note: Using array_unshift() would've been easier though]]
if you wanna call this function using like:
<?php
$add = call_user_func_array('mysqli_stmt_bind_result', $data);
?>
you'll need to add the the prepared statement links in the $data array;
the easiest wy to add a string on the beginning of an array I found was like the below, reversing the array, adding the string and then reversing the array again to get a proper order...
array_unshift() only excepts arrays and typecasting didn't work for me. In my class this would look like:
<?
$data = array_reverse($data);
$data[] = $this->prepared;
$data = array_reverse($data);
?>
Hope it's usefull to anyone...


andrey

[ATTENTION] When connecting with 4.1.x libmysql (on windows there is no other option) to 5.0 (5.1) MySQL server there is incompatibility in the protocol regarding type DECIMAL. Therefore result binding on decimal column is not possible. In this case normal mysqli_query() has to be used!!!
When trying to bind you will get FALSE if one of the columns is DECIMAL.


michael newton - http://mike.eire.ca/

Windows users:
If you're having problems with this statement in general (not just with DECIMAL columns) try the latest client library from MySQL at http://dev.mysql.com/downloads/connector/php/ to see if it helps.
I couldn't even get the example code above working (using the very latest PHP and MySQL) but swapping in the later DLL files from MySQL fixed everything up.


andrey

If you select LOBs use the following order of execution or you risk mysqli allocating more memory that actually used
1)prepare()
2)execute()
3)store_result()
4)bind_result()
If you skip 3) or exchange 3) and 4) then mysqli will allocate memory for the maximal length of the column which is 255 for tinyblob, 64k for blob(still ok), 16MByte for MEDIUMBLOB - quite a lot and 4G for LONGBLOB (good if you have so much memory). Queries which use this order a bit slower when there is a LOB but this is the price of not having memory exhaustion in seconds.


thejkwhosaysni

I've created these functions which will act like mysqli_fetch_array() and mysqli_fetch_object() but work with bound results.
<?
function fetch_object() {
$data = mysqli_stmt_result_metadata($this->stmt);
$count = 1; //start the count from 1. First value has to be a reference to stmt.
$fieldnames[0] = &$this->stmt;
$obj = new stdClass;
while ($field = mysqli_fetch_field($data)) {
$fn = $field->name; //get all the feild names
$fieldnames[$count] = &$obj->$fn; //load the fieldnames into an object..
$count++;
}
call_user_func_array(mysqli_stmt_bind_result, $fieldnames);
mysqli_stmt_fetch($this->stmt);
return $obj;
}
function fetch_array() {
$data = mysqli_stmt_result_metadata($this->stmt);
$count = 1; //start the count from 1. First value has to be a reference to the stmt. because bind_param requires the link to $stmt as the first param.
$fieldnames[0] = &$this->stmt;
while ($field = mysqli_fetch_field($data)) {
$fieldnames[$count] = &$array[$field->name]; //load the fieldnames into an array.
$count++;
}
call_user_func_array(mysqli_stmt_bind_result, $fieldnames);
mysqli_stmt_fetch($this->stmt);
return $array;
}
?>
Hope this helps some people, I was puzzled by this for a while.


matti

Hi
I saw a bit of discussion about using mysqli_stmt_bin_result dynamically, without knowing exactly how many columns will be returned.
After a while i developed this snippet to mimic the same behaviour as mysql_fetch_array():
<?php
# of fields in result set.
$nof = mysqli_num_fields( mysqli_stmt_result_metadata($handle) );
# The metadata of all fields
$fieldMeta = mysqli_fetch_fields( mysqli_stmt_result_metadata($handle) );
       
# convert it to a normal array just containing the field names
$fields = array();
for($i=0; $i < $nof; $i++)
   $fields[$i] = $fieldMeta[$i]->name;
# The idea is to get an array with the result values just as in mysql_fetch_assoc();
# But we have to use call_user_func_array to pass the right number of args ($nof+1)
# So we create an array:
# array( $stmt, &$result[0], &$result[1], ... )
# So we get the right values in $result in the end!
# Prepare $result and $arg (which will be passed to bind_result)
$result = array();
$arg = array($this->stmt);
for ($i=0; $i < $nof; $i++) {
   $result[$i] = '';
   $arg[$i+1] = &$result[$i];
}
call_user_func_array ('mysqli_stmt_bind_result',$arg);
# after mysqli_stmt_fetch(), our result array is filled just perfectly,
# but it is numbered (like in mysql_fetch_array() ), not indexed by field name!
# If you just want to mimic that ones behaviour you can stop here :)
mysqli_stmt_fetch($this->stmt);
# Now you can use $result
print_r($result);
# But beware! when using the fetch in a loop, always COPY $result or else you might
# end with all the same values because of the references
?>
Hope that this will help someone....
Matt


brad dot jackson

A potential problem exists in binding result parameters from a prepared statement which reference large datatypes like mediumblobs.  One of our database tables contains a table of binary image data.  Our largest image in this table is around 50Kb, but even so the column is typed as a mediumblob to allow for files larger than 64Kb.  I spent a frustrating hour trying to figure out why mysqli_stmt_bind_result choked while trying to allocate 16MB of memory for what should have been at most a 50Kb result, until I realized the function is checking the column type first to find out how big a result _might_ be retrieved, and attempting to allocate that much memory to contain it.  My solution was to use a more basic mysqli_result() query.  Another option might have been to retype the image data column as blob (64Kb limit).

Change Language


Follow Navioo On Twitter
mysqli_affected_rows
mysqli_autocommit
mysqli_bind_param
mysqli_bind_result
mysqli_change_user
mysqli_character_set_name
mysqli_client_encoding
mysqli_close
mysqli_commit
mysqli_connect_errno
mysqli_connect_error
mysqli_connect
mysqli_data_seek
mysqli_debug
mysqli_disable_reads_from_master
mysqli_disable_rpl_parse
mysqli_dump_debug_info
mysqli_embedded_server_end
mysqli_embedded_server_start
mysqli_enable_reads_from_master
mysqli_enable_rpl_parse
mysqli_errno
mysqli_error
mysqli_escape_string
mysqli_execute
mysqli_fetch_array
mysqli_fetch_assoc
mysqli_fetch_field_direct
mysqli_fetch_field
mysqli_fetch_fields
mysqli_fetch_lengths
mysqli_fetch_object
mysqli_fetch_row
mysqli_fetch
mysqli_field_count
mysqli_field_seek
mysqli_field_tell
mysqli_free_result
mysqli_get_charset
mysqli_get_client_info
mysqli_get_client_version
mysqli_get_host_info
mysqli_get_metadata
mysqli_get_proto_info
mysqli_get_server_info
mysqli_get_server_version
mysqli_get_warnings
mysqli_info
mysqli_init
mysqli_insert_id
mysqli_kill
mysqli_master_query
mysqli_more_results
mysqli_multi_query
mysqli_next_result
mysqli_num_fields
mysqli_num_rows
mysqli_options
mysqli_param_count
mysqli_ping
mysqli_prepare
mysqli_query
mysqli_real_connect
mysqli_real_escape_string
mysqli_real_query
mysqli_report
mysqli_rollback
mysqli_rpl_parse_enabled
mysqli_rpl_probe
mysqli_rpl_query_type
mysqli_select_db
mysqli_send_long_data
mysqli_send_query
mysqli_server_end
mysqli_server_init
mysqli_set_charset
mysqli_set_local_infile_default
mysqli_set_local_infile_handler
mysqli_set_opt
mysqli_slave_query
mysqli_sqlstate
mysqli_ssl_set
mysqli_stat
mysqli_stmt_affected_rows
mysqli_stmt_attr_get
mysqli_stmt_attr_set
mysqli_stmt_bind_param
mysqli_stmt_bind_result
mysqli_stmt_close
mysqli_stmt_data_seek
mysqli_stmt_errno
mysqli_stmt_error
mysqli_stmt_execute
mysqli_stmt_fetch
mysqli_stmt_field_count
mysqli_stmt_free_result
mysqli_stmt_get_warnings
mysqli_stmt_init
mysqli_stmt_insert_id
mysqli_stmt_num_rows
mysqli_stmt_param_count
mysqli_stmt_prepare
mysqli_stmt_reset
mysqli_stmt_result_metadata
mysqli_stmt_send_long_data
mysqli_stmt_sqlstate
mysqli_stmt_store_result
mysqli_store_result
mysqli_thread_id
mysqli_thread_safe
mysqli_use_result
mysqli_warning_count
eXTReMe Tracker