Performance of loading fields I wrote a class with this function. Can I improve my code performance in the bottom of the function? I don't like the while loops!

public function loadFields(array $fields, $as_object = Config::FIELDS_AS_OBJECT) {
        $connection = $this-&gt;connection;
        $sql = "SELECT ".implode(",", $fields)." FROM ".$this-&gt;table." WHERE lang = ?";
        if (!($stmt = $connection-&gt;prepare($sql))){
            throw new MysqliPrepareException($connection);
        }
        if(!($stmt-&gt;bind_param('s',$this-&gt;lang))){
            throw new MysqliBindParamException($connection);
        }
        if(!$stmt-&gt;execute()){
            throw new MysqliExecuteException($connection);
        }
        $stmt-&gt;store_result();
        $variables = array();
        $data = array();
        $meta = $stmt-&gt;result_metadata();
        while ($field = $meta-&gt;fetch_field()) {
            $variables[] = &amp;$data[$field-&gt;name];
        }
        call_user_func_array(array($stmt, 'bind_result'), $variables);
        $i = 0;
        while ($stmt-&gt;fetch()) {
            $array[$i] = array();
            foreach ($data as $k =&gt; $v) {
                $array[$i][$k] = $v;
            }
            $i++;
        }
        $array = (!isset($array[1]) ? $array[0] : $array);
        return ($as_object == true ? (object) $array : $array);
}
I'd imagine a $stmt-&gt;fetchAll() would be more performant than the while-loop and $stmt-&gt;fetch(). Rather than paging over an entire set of records one at a time, try returning the results from the database in-bulk. Your code is making a trip to the database with each iteration of your while loop. Your code will still look virtually identical:

$i = 0;
$r = $stmt-&gt;fetchAll();
foreach ($r as $data) {
    $array[$i] = array();
    foreach ($data as $k =&gt; $v) {
        $array[$i][$k] = $v;
    }
    $i++;
}  


If you're using mysqli (native driver only), and not PDO's MySQL, you could do this:

$i = 0;
$r = $stmt-&gt;get_result();
$r = $r-&gt;fetchAll();
foreach ($r as $data) {
    $array[$i] = array();
    foreach ($data as $k =&gt; $v) {
        $array[$i][$k] = $v;
    }
    $i++;
}  


You shouldn't worry about loops per se, but performance hits that you see, whether they come from loops or not. This is only possible if you're profiling your code.