Welcome Guest, Not a member yet? Register   Sign In
Display results of db query with only one result
#1

[eluser]mb2007[/eluser]
Hello,

often I just request for one special database entry, so I use LIMIT 1. But the result with the CI function is a object so I use a foreach loop to display it. Is there a better way to access the array?
Code:
$this->db->select('some_row');
$this->db->where('some_other_row',$some_other_row_value);
$this->db->limit(1);
$query = $this->db->get('table');

foreach($query->result() as $row):
    echo $row->some_row;
endforeach;
#2

[eluser]litzinger[/eluser]
I'd like to know this as well. Seems silly to loop through a single result :/
#3

[eluser]codex[/eluser]
[quote author="mb2007" date="1187120212"]Hello,

often I just request for one special database entry, so I use LIMIT 1. But the result with the CI function is a object so I use a foreach loop to display it. Is there a better way to access the array?
Code:
$this->db->select('some_row');
$this->db->where('some_other_row',$some_other_row_value);
$this->db->limit(1);
$query = $this->db->get('table');

foreach($query->result() as $row):
    echo $row->some_row;
endforeach;
[/quote]

Hey,

this is what I struggled with the first time I started using CI (2 days ago).

Try this:

Code:
$this->db->select('some_row');
$this->db->from('table');
$this->db->where('some_other_row',$some_other_row_value);
$query = $this->db->get();

$query->result();

echo $query[0]->some_row;
#4

[eluser]mb2007[/eluser]
Hej,

thanks. It's not complete correct, but so I found the correct solution.
Code:
$this->db->select('some_row');
$this->db->from('table');
$this->db->where('some_other_row',$some_other_row_value);
$query = $this->db->get();
$result = $query->result();
echo $result[0]->some_row;
Or a bit shorter:
Code:
$this->db->select('some_row');
$this->db->from('table');
$this->db->where('some_other_row',$some_other_row_value);
$result = $this->db->get()->result();
echo $result[0]->some_row;
#5

[eluser]codex[/eluser]
Yeah, I kinda knew I was doing it wrong, but you got the idea ;-)
#6

[eluser]Michael Wales[/eluser]
Code:
$query = $this->db->get();
$result = $query->row();
echo "omg, must scroll all the way to the 3rd heading within the documentation";
echo $this->session->userdata('username') . " dies";
echo "<a href=\"http://www.ellislab.com/codeigniter/user-guide/database/results.html\">User Guide: Generating Query Results</a>";
#7

[eluser]codex[/eluser]
[quote author="walesmd" date="1187128457"]
Code:
$query = $this->db->get();
$result = $query->row();
echo "omg, must scroll all the way to the 3rd heading within the documentation";
echo $this->session->userdata('username') . " dies";
echo "<a href=\"http://www.ellislab.com/codeigniter/user-guide/database/results.html\">User Guide: Generating Query Results</a>";
[/quote]

Aah, you gotta love those sarcastic comments ;-)
#8

[eluser]Michael Wales[/eluser]
I'm just looking forward to the day when I register for someone's website and I see those values echo'd out in the footer. Tongue
#9

[eluser]codex[/eluser]
Although CI is very well documented, it's still a lot of info to absorb and it's easy to miss (fairly obvious) stuff like this. So, please be gentle to us noobs.
#10

[eluser]mb2007[/eluser]
Sorry, didn't saw that.




Theme © iAndrew 2016 - Forum software by © MyBB