I found the following on http://us2.php.net/manual/en/function.mysqli-stmt-bind-result.php
Haven't tried it but it seems to make sense to me. I've used the
call_user_func_array() to call bind_param with a variable number of
arguments, seems like it would work for bind_result too.
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