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->connection;
$sql = "SELECT ".implode(",", $fields)." FROM ".$this->table." WHERE lang = ?";
if (!($stmt = $connection->prepare($sql))){
throw new MysqliPrepareException($connection);
}
if(!($stmt->bind_param('s',$this->lang))){
throw new MysqliBindParamException($connection);
}
if(!$stmt->execute()){
throw new MysqliExecuteException($connection);
}
$stmt->store_result();
$variables = array();
$data = array();
$meta = $stmt->result_metadata();
while ($field = $meta->fetch_field()) {
$variables[] = &$data[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $variables);
$i = 0;
while ($stmt->fetch()) {
$array[$i] = array();
foreach ($data as $k => $v) {
$array[$i][$k] = $v;
}
$i++;
}
$array = (!isset($array[1]) ? $array[0] : $array);
return ($as_object == true ? (object) $array : $array);
}