Q:

From Query Result to Associative Array Codeigniter

I'm writing a new class to handle my db and I would really like to implement some custom feature that I usually do while programming.
One of those feature is the possibility to fetch from a query two field, in something like this:
SELECT brandId,brandName FROM Brands;

And has a result an associative array indexed as my brandId and with value brandName.
This is what i tried so far but the application seems to get stuck and be unable to produce any error message, just finishless loading 
function getColHash($query){
    $output = array();
    while ($result = $this->query($query)->result()) {
        $output[$result[0]] = $result[1];

    }
    return $output;
  }

Some hint would be really appreciated

A:

$result = $this->query($query)->result(); executes the query and fetches all the results into $result. As long as the query returns any rows, executing that in a while loop condition like that will be infinite.
You can get the results first and then iterate them to generate your output. Also, it looks like result() returns an array of objects, so you probably want result_array() instead.
$results = $this->query($query)->result_array();

foreach ($results as $result) {
    $output[$result[0]] = $result[1];
}

I'm not sure if Codeigniter returns result arrays with numerical indexes as well as string ones, so you may need to set $result = array_values($result) before you can refer to $result[0] and $result[1].

