note 55090 added to function.mysqli-stmt-bind-result

From: Date: Mon, 25 Jul 2005 14:30:42 +0000
Subject: note 55090 added to function.mysqli-stmt-bind-result
Groups: php.notes 
Request: Send a blank email to php-notes+get-92417@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/function.mysqli-stmt-bind-result.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55090 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55090&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55090&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55090&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55090&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55090&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55090&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55090&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#92417) next »