1

I am using codeigniter to get a single result from the db, like this:

$user = $this->db->query("SELECT * FROM users LIMIT 1");

I am then getting the returned object and looping trough it, this way:

foreach ($user->result() as $row) { ... }

As you can imagine, there is not point in using the loop, since there is only one result. Is there a way to get users parameters without looping? Something among this line:

echo $user['name'];

Thanks in advance.

4 Answers 4

9
$user = $this->db->query("SELECT * FROM users LIMIT 1")->row();

for having it as an object ( echo $user->name;)

$user = $this->db->query("SELECT * FROM users LIMIT 1")->row_array();

for having is as an array ( echo $user['name']; );

$user = $this->db->limit(1)->get('users')->row(); //or ->row_array();

using AR;

$row = $user->first_row();

to get the first row out of a result set ($this->db->result()), as an object (default);

$row = $user->first_row('array');

to get the first row out of a result set, as an array;

$row = $user->row_array(1);

to return a specific row (first, in this case. Just pass the <N> of the row). Works also for $user->row(1);

Sign up to request clarification or add additional context in comments.

Comments

1

It would be:

$user = $this->db->query("SELECT * FROM users LIMIT 1")->row_array();

More details here: http://ellislab.com/codeigniter/user-guide/database/results.html

Comments

0

do something like this

    //in the model
    $query = $this->db->query("your query");

$row = $query->row();

    if (isset($row))
return $row;
}else{
return false;
}

then echo your results like this

echo $row->username;

if you want to pass it to a view

//in the controller

$this->load->model('somemodel');
        $hm=$this->somemodel->somemethod();
        $data['query']=$hm;
    $this->load->view('path/to/view',$data);

Then echo the same way

echo $row->username;

Comments

-1

Suppose for example your model function is like this.......

 public function get_users()
 {

  $query = "Select * from users limit 1";

  $res=$this->db->query($query);

    if($res->num_rows()>0){
        return $res->result("array");
    }
    return array();
 }

Suppose you are calling this model in your controller function like this

    $users =  $this->model_file->get_users();

then you can echo like this echo $users[0]['user_name'];

3 Comments

I am not facing any problems with that kind of syntax. I used it for all queries through out my application.
I never saw it mentioned; I just see this: You can also pass a string to result() which represents a class to instantiate for each result object (note: this class must be loaded)
Don't worry I don't have that basic knowledge in PHP & CI. Any way leave it like that but it's not wrong that i can say ........

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.