Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have just started using CodeIgniter and want to get data from database using $query->result(), but without a foreach loop. Here is my current code:

   $this->db->select('m_name');
   $query1 = $this->db->get("marchant_details",1);
   $rows1 = $query1->result();

However, I don't want to use a foreach loop like this, to retrieve the data:

   foreach($query1->result() as $rows1)
   {
       $name[] = $rows1->m_name;
   }

Can anyone offer an alternative solution?

share|improve this question
1  
i suppose do you want to take only the first result? –  Luigi De Rosa Aug 8 at 21:37
 
If you are limiting the SQL query, and fetching the first row only, why did you use a loop to create a one-index $name array? –  Hashem Qolami Aug 8 at 21:48
 
I see you want the first result only. Use row() from ellislab.com/codeigniter/user-guide/database/results.html –  machineaddict Aug 9 at 6:03
 
I've updated my answer, I think one point is not considered in the answers. –  Hashem Qolami Aug 9 at 8:13

4 Answers

I just did a quick search. I'm going to sleep, but can you try something like this:

$query->result_array();

If it doesn't work, I'll check tomorrow.

source: user-guide Maybe it will come handy.

share|improve this answer

There are two assumptions: either I misunderstood the question or the others did.

The point: by passing 1 as second parameter to $this->db->get(); method, it sets LIMIT 1 to the query and you'll get only 1 row as result.

So, why should you need to use a loop on a single row db result?

If you use $query->result(); the result would be something like:

Array
(
    [0] => stdClass Object
        (
            [m_name] => Foo
        )

)

To get the m_name value you can do the following:

$result = $query->result();
echo $result[0]->m_name;

By any reason, if you need a numeric array contains the value (as you did it in your loop) you can simply do it by $name[] = $result[0]->m_name;.

And if you use $query->result_array(); the result would be something like:

Array
(
    [0] => Array
        (
            [m_name] => Foo
        )

)

To get the m_name value you can do the following:

$result = $query->result_array();
echo $result[0]['m_name'];

But if you stop limiting the query, there are multiple rows in your query result, you can use rows1 = $query1->result_array(); instead. and array_map() to manipulate the elements of the given array:

$rows1 = $query1->result_array();

// `$row1` will be something like this:
Array
(
    [0] => Array
        (
            [m_name] => Foo
        )
    [1] => Array
        (
            [m_name] => Bar
        )
    [2] => Array
        (
            [m_name] => Baz
        )

)

Use array_map() to manipulate the result array:

function getName($array) {
    return $array['m_name'];
}

$array = array_map("getName", $rows1);

print_r($array);

If you are using PHP v5.3+ you do the following:

$array = array_map(function($array) {
    return $array['m_name'];
}, $rows1);
share|improve this answer

Well there is the official page in the CodeIgniter User Guide for generating DB query results depicting variations like

$row = $query->first_row()
$row = $query->last_row()
$row = $query->next_row()
$row = $query->previous_row()

If you work with CodeIgniter, its charm is that it effectively quite nicely documented.

I would assume you can go further down the abstraction layers if that's what you want, respectively you can not use the DB class but the class of your choice as custom library or whatever.

Why then are you not happy with the given possibilities, respectively how do you want to generate your query results?

share|improve this answer

you can do like this

$data=array();    
$this->db->select('m_name');
$query1 = $this->db->get("marchant_details",1);
$data['name'] = $query1->result_array();
return $data;

and for more information

result_array();  // For multiple rows
row_array();     // For one row
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.