Welcome Guest, Not a member yet? Register   Sign In
Noob: Dealing with a Single Query Result
#1

[eluser]ebohling[/eluser]
I know there has to be a better way to access a single db result (select opportunity_name from an id) then this:

$this->db->select('opportunity_name');
$this->db->from('Opportunities');
$this->db->where('opportunity_id', (int)$new->contribution_opportunity_id);
$query = $this->db->get();

foreach ($query->result() as $row) // there has to be a better, more efficient way!
{
$new->opportunity_name = $row->opportunity_name;
}

But after scouring the forums and trying my darnedest, I cannot figure it help.
#2

[eluser]n0xie[/eluser]
Try this (don't need the foreach loop) :
Code:
$query->row()->opportunity_name;
$query->row('opportunity_name');

// or in php5 using chaining:
$query = $this->db->get()->row()->opportunity_name;
Take a look here: Query results
#3

[eluser]ebohling[/eluser]
while i could not get your sample to work (got same convert error as i was getting when i was trying), but i followed your link and came up with this:

$this->db->select('opportunity_name');
$this->db->from('Opportunities');
$this->db->where('opportunity_id', (int)$new->contribution_opportunity_id);
$query = $this->db->get();

if ($query->num_rows() > 0)
{
$row = $query->row();
$new->opportunity_name = $row->opportunity_name;
}
else
{
$new->opportunity_name = 'an opportunity was not selected';
}
#4

[eluser]Thorpe Obazee[/eluser]
[quote author="ebohling" date="1245218478"]while i could not get your sample to work (got same convert error as i was getting when i was trying)
[/quote]
You're probably using PHP 4.
[quote author="ebohling" date="1245218478"]
Code:
$this->db->select('opportunity_name');
$this->db->from('Opportunities');
$this->db->where('opportunity_id', (int)$new->contribution_opportunity_id);
$query = $this->db->get();
            
if ($query->num_rows() > 0)
{
  $row = $query->row();
  $new->opportunity_name = $row->opportunity_name;
}
else
{
  $new->opportunity_name = 'an opportunity was not selected';
}
[/quote]

Try to put your code in [ code] tags
#5

[eluser]ebohling[/eluser]
no, i'm using php5




Theme © iAndrew 2016 - Forum software by © MyBB