Welcome Guest, Not a member yet? Register   Sign In
result() and result_array()
#1

[eluser]pendalpusher[/eluser]
I am using active records to produce results from my db.

When I run a query that only has a single row in it.
I get something like this as the result.

Code:
Array ( [0] => stdClass Object ( [id] => 2098 [quote_number] => 611,[markup] => 0 [won_lost] => 0 [quote_date] => 2010-05-05 [active] => 1 ) )

How do I get rid of that first [0]? I would like to keep the return of the function as an object, but now I have to reference it like this..

Code:
<?=result[0]->id;?>
<?=result[0]->quote_number;?>

Can anyone help?

Thanks,
Pendal
#2

[eluser]danmontgomery[/eluser]
If you're only expecting one row back, use row() instead of result()

Code:
$query = $this->db->limit(1)->get('my_table');
if($query) {
    $row = $query->row()
    echo $row->id;
    echo $row->quote_number;
    // etc
#3

[eluser]pendalpusher[/eluser]
thanks, i knew it had to be easy .. this is what I ended up doing since the call may or may not return more than one row.

Code:
if($query->num_rows() > 1)
{
$result = $query->result();
}
elseif($query->num_rows() == 1)
{
$result = $query->row();
}
else
{
$result = FALSE;
}

Thanks again for the quick help.
P




Theme © iAndrew 2016 - Forum software by © MyBB