如何在没有foreach循环的codeigniter中使用$ query-> result()从数据库中获取数据?

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?

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);

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.

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?

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