0
SELECT col FROM table

Say I have this query and I get the following result:

array(2) {
0 => array(1){
"COL"=>"value1"
},
1 => array(1){
"COL"=>"value2"
}
}

Is there some SQL statement I can use to get this from the same data:

array(2) {
0=>"value1",
1=>"value2"
}

I'm using zend framework and db2. If it can't be done through SQL, can it be done with Zend?

Thanks

EDIT: I should clarify, I can do this with a loop or a function. I was just wondering if there was a built-in way to do it to save a few lines of code and be all fancy smartpants.

1
  • Which DB2 platform are you using? DB2 for i, LUW, or z/OS? Commented Jun 20, 2013 at 14:38

4 Answers 4

2

you could try the fetchPairs() method. That might be what you're looking for, at least in a limited fashion.

The fetchPairs() method returns data in an array of key-value pairs, as an associative array with a single entry per row. The key of this associative array is taken from the first column returned by the SELECT query. The value is taken from the second column returned by the SELECT query. Any other columns returned by the query are discarded.

You may also try and set the fetch method to Zend_Db::FETCH_COLUMN.

Zend_Db::FETCH_COLUMN: return data in an array of values. The value in each array is the value returned by one column of the result set. By default, this is the first column, indexed by 0.

$db->setFetchMode(Zend_Db::FETCH_COLUMN);
Sign up to request clarification or add additional context in comments.

Comments

2

try a custom function like this:-

function myfuntion($array) { 
  if (!is_array($array)) { 
     return FALSE; 
   } 
 $result = array(); 
 foreach ($array as $key => $value) { 
  if (is_array($value)) { 
  $result = array_merge($result, myfuntion($value)); 
   } 
   else { 
   $result[$key] = $value; 
  } 
 } 
return $result; 
 } 

Comments

0

Here is how I do it:

in my model:

public function getRecords() {
$select = $this->select();

return $this->fetchAll($select);
}

in my controller:

$records = $mdl->getRecords();
$recordsArray = $records->toArray();

1 Comment

Not working for me. Says my result is not an object. I'm using: $result = $db->fetchAll($query, array($condition)); Then return $result->toArray();
0

I think the easiest way is :

 public function my_function() 
   { 

      $db = Zend_Db_Table::getDefaultAdapter();
      $results = $db->fetchAll("SELECT * FROM my_table");
      /* if you want only one row, do something like this*/
      /* $results = $db->fetchRow("SELECT * FROM my_table") */
      return $results;
    }
/* Then maybe in your controller you can call the function 
and */
$results=my_function();
/*make values avelaible to the view*/
$this->view->$results=$results

Then inside the view : it depends on what your function resturns: case: its fetchAll:

<?php foreach ($this->results as $item): ?>
    <?=$item['address']?> /* access fields */
 <?php endforeach ?>

case: its fetchRow: No need to use foreach(). I hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.