update page now

Voting

: zero minus zero?
(Example: nine)

The Note You're Voting On

nieprzeklinaj at gmail dot com
14 years ago
I wrote a function that fetches all rows from a result set - either normal or prepared.

<?php
function fetch($result)
{    
    $array = array();
    
    if($result instanceof mysqli_stmt)
    {
        $result->store_result();
        
        $variables = array();
        $data = array();
        $meta = $result->result_metadata();
        
        while($field = $meta->fetch_field())
            $variables[] = &$data[$field->name]; // pass by reference
        
        call_user_func_array(array($result, 'bind_result'), $variables);
        
        $i=0;
        while($result->fetch())
        {
            $array[$i] = array();
            foreach($data as $k=>$v)
                $array[$i][$k] = $v;
            $i++;
            
            // don't know why, but when I tried $array[] = $data, I got the same one result in all rows
        }
    }
    elseif($result instanceof mysqli_result)
    {
        while($row = $result->fetch_assoc())
            $array[] = $row;
    }
    
    return $array;
}
?>

Simply call it passing a result set or executed statement and you'll get all rows fetched.

<< Back to user notes page

To Top