BigJobbies
BigJobbies

Reputation: 4343

CodeIgniter - Returning column result instead of entire row

I have a query which im trying to return the exact entry for, but instead it keeps returning the 'Array' ... how do i extract just the one 'name' in looking for?

My current query looks like so

$query = $this->db->query('SELECT name FROM '.$this->table_name.' WHERE id =1');
return $query->result();

Basically, i just want to return the actual name, not an array

Upvotes: 0

Views: 3817

Answers (1)

marramgrass
marramgrass

Reputation: 1411

You need to dig into the objects returned by the query. row() returns the first row in a result set:

$row = $query->row();
return $row->name;

result() returns an array of objects representing the rows in the result set. You need to get the object itself, then get it's 'name' property.

It's also worth checking $query->num_rows() to make sure you have results:

if ($query->num_rows() > 0) {...}

Upvotes: 1

Related Questions