0

I am trying to bind the results generated by $stmt -> fetch() into an array. One bad solution is I put it in while loop and get result in array from there. The only reason I asked this question because I cannot use mysqli_result::fetch_array() or mysqli_result::fetch_assoc(). , get_result() as its in PHP 5.3 and its not supported by my server. Kindly let me know is there any better way to do it with shortcodes like get_result()

I tried this but no luck so far.

$stmt->execute();     

/* Bind results */
$stmt -> bind_result($abc);

/* Fetch the value */
$row = $stmt -> fetch();
$row1=$row;
Robbert
  • 6,481
  • 5
  • 35
  • 61
user3027531
  • 282
  • 1
  • 5
  • 20

1 Answers1

-1

I'm using this alternative code (because I can't use get_result either), which I found here: https://stackoverflow.com/a/8882407/1930721

<?php
$stmt = $db->prepare( ' YOUR SQL QUERY ' ) ); /* edit to your needs */
$stmt->bind_param( ... ); /* edit to your needs */

$stmt->execute();

$meta = $stmt->result_metadata();
$fields = $result = array();
while ($field = $meta->fetch_field()) { 
    $var = $field->name; 
    $$var = null; 
    $fields[$var] = &$$var; 
}
call_user_func_array(array($stmt,'bind_result'),$fields);
$i = 0;
while ($stmt->fetch()) {
    $result[$i] = array();
    foreach($fields as $k => $v)
        $result[$i][$k] = $v;
    $i++;
}
?>
Community
  • 1
  • 1
GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59